Skip to content

Instantly share code, notes, and snippets.

@monkindey
Created July 30, 2017 14:39
Show Gist options
  • Save monkindey/bcf4e110845fea1f392afe71dbab8672 to your computer and use it in GitHub Desktop.
Save monkindey/bcf4e110845fea1f392afe71dbab8672 to your computer and use it in GitHub Desktop.
utf-8 buffer gbk
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