Last active
September 17, 2020 00:57
-
-
Save zzjtnb/23d696b08692b13313e203356210e028 to your computer and use it in GitHub Desktop.
Node中的字符编码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Base64/hex | |
| Base64与hex都是将二进制数据编码成字符串的编码方式,针对的是二进制字节而非字符串。 | |
| 因此,要获得某个string的base64编码时,应该先得到string的二进制,再对二进制进行base6编码: | |
| ```JavaScript | |
| // 编码 | |
| const str = '哈哈' | |
| let b = Buffer.from(str) // b中存的是哈哈的utf8编码二进制 | |
| let base64Code = b.toString('base64') // '5ZOI5ZOI' | |
| // 解码 | |
| const code = '5ZOI5ZOI' | |
| let str = Buffer.from('5ZOI5ZOI', 'base64') // '哈哈' | |
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Base64/hex | |
| Base64与hex都是将二进制数据编码成字符串的编码方式,针对的是二进制字节而非字符串。 | |
| 因此,要获得某个string的base64编码时,应该先得到string的二进制,再对二进制进行base6编码: | |
| ```JavaScript | |
| // 编码 | |
| const str = '哈哈' | |
| let b = Buffer.from(str) // b中存的是哈哈的utf8编码二进制 | |
| let base64Code = b.toString('base64') // '5ZOI5ZOI' | |
| // 解码 | |
| const code = '5ZOI5ZOI' | |
| let str = Buffer.from('5ZOI5ZOI', 'base64') // '哈哈' | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment