Skip to content

Commit c3d4e41

Browse files
committed
modify buffer code
1 parent f9caee8 commit c3d4e41

File tree

5 files changed

+89
-11
lines changed

5 files changed

+89
-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('信息有误!');

06-buffer/07-show-utf-code.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env node
2+
3+
const dec2Hex = num => num.toString(16);
4+
5+
function utf8Code(str) {
6+
const code = encodeURI(str)
7+
.replace(/%/g, ' ')
8+
.trim();
9+
return code == str ? utf16Code(str).slice(3) : code;
10+
}
11+
12+
const utf8 = new TextEncoder();
13+
function utf8CodeV2(str) {
14+
return Array.from(utf8.encode(str))
15+
.map(dec2Hex)
16+
.join(' ')
17+
.toUpperCase();
18+
}
19+
20+
function utf16Code(str) {
21+
const code = dec2Hex(str.charCodeAt(0))
22+
.toUpperCase()
23+
.replace(/(\w{2})/g, '$1 ')
24+
.trim();
25+
return code.length == 2 ? '00 ' + code : code;
26+
}
27+
28+
function getCodeInfo(c) {
29+
return {
30+
"char": c,
31+
"UTF-8": utf8Code(c),
32+
"UTF-16": utf16Code(c),
33+
};
34+
}
35+
36+
function main() {
37+
const msg = 'hello, 王顶';
38+
const codes = [...msg].map(getCodeInfo);
39+
console.table(codes);
40+
}
41+
42+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
3+
const dec2Hex = num => num.toString(16);
4+
5+
String.prototype.toUTF8Code = function () {
6+
const code = encodeURI(this[0])
7+
.replace(/%/g, ' ')
8+
.trim();
9+
return (code == this[0]) ? this[0].toUTF16Code().slice(3) : code;
10+
}
11+
12+
String.prototype.toUTF16Code = function() {
13+
const code = dec2Hex(this.charCodeAt(0))
14+
.toUpperCase()
15+
.replace(/(\w{2})/g, '$1 ')
16+
.trim();
17+
return code.length == 2 ? '00 ' + code : code;
18+
}
19+
20+
function getCodeInfo(c) {
21+
return {
22+
"char": c,
23+
"UTF-8": c.toUTF8Code(),
24+
"UTF-16": c.toUTF16Code(),
25+
};
26+
}
27+
28+
function main() {
29+
const msg = 'hello, 王顶';
30+
const codes = [...msg].map(getCodeInfo);
31+
console.table(codes);
32+
}
33+
34+
main();

0 commit comments

Comments
 (0)