Skip to content

Commit 4605035

Browse files
committed
modify buffer code
1 parent d76b434 commit 4605035

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

02-global/02-base64.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

06-buffer/02-encode.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ const log = console.log,
55
pwd = process.argv[3];
66

77
if(process.argv.length !== 4) {
8-
console.error('命令行格式:cmd username password');
8+
console.error('usage:cmd username password');
99
process.exit(1);
1010
}
1111

1212
log('user name: %s\npassword: %s', usr, pwd);
1313

14-
const buf = Buffer.from(usr + ':' + pwd);
14+
// method A
15+
log('Base64:', btoa(usr + ':' + pwd));
1516

17+
// method B
18+
const buf = Buffer.from(usr + ':' + pwd);
1619
log('Base64:', buf.toString('Base64'));

06-buffer/03-decode.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#!/usr/bin/env node
22

33
if(process.argv.length !== 3) {
4-
console.error('命令行格式:cmd base64_string');
4+
console.error('usage:cmd base64_string');
55
process.exit(1);
66
}
77

8+
// method A
9+
const buf = atob(process.argv[2]);
10+
const info = buf.split(':');
11+
12+
// method B
13+
/*
814
const buf = Buffer.from(process.argv[2], 'base64');
915
const info = buf.toString('utf8').split(':');
16+
*/
1017

1118
if(info.length !== 2) {
1219
console.error('信息有误!');

0 commit comments

Comments
 (0)