Created
July 30, 2017 14:39
-
-
Save monkindey/bcf4e110845fea1f392afe71dbab8672 to your computer and use it in GitHub Desktop.
utf-8 buffer gbk
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
const iconv = require('iconv-lite'); | |
const diaoEn = 'diao'; | |
const diao = '很屌'; | |
const diaoUTFBuf = Buffer.from(diao); | |
// 用utf-8编码的很屌, 然后用gbk去解码, 导致乱码了 | |
const decoded = iconv.decode(diaoUTFBuf, 'gbk'); | |
// mojibake(乱码) | |
console.log('很屌', decoded); | |
// '很屌'的utf-8编码, 中文占了三个字符 | |
console.log('很屌', diaoUTFBuf); | |
// 'diao'的utf-8编码, 英文只占了一个字符 | |
console.log('邮箱', Buffer.from(diaoEn)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment