Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utf-8 to gbk is error #78

Closed
jincdream opened this issue Sep 9, 2014 · 4 comments
Closed

utf-8 to gbk is error #78

jincdream opened this issue Sep 9, 2014 · 4 comments

Comments

@jincdream
Copy link

fs.readFile(path,{encoding:'utf-8'},function(err,data){
buf = iconv.encode(data,'gbk');
fs.writeFile(outName,buf,function(){
callback();
});
});

input: var s = 1;
ouput : ?var s = 1;

A '?' is always there.

@jincdream
Copy link
Author

now I use this way to solve:
fs.readFile(path,{encoding:'utf-8'},function(err,data){
buf = iconv.encode(data,'gb2312');
console.log();
fs.writeFile(outName,buf,function(){
iconv.extendNodeEncodings();
var str = fs.readFileSync(outName,'gb2312');
str = str.substring(1);
fs.writeFileSync(outName,str,'gb2312');
iconv.undoExtendNodeEncodings();
});
});

@ashtuchkin
Copy link
Owner

Could you post:

  • the contents of the file you convert
  • contents of 'data' variable in your first script?
  • version of node and iconv-lite you use

@ashtuchkin
Copy link
Owner

Although I probably know what the problem is. The file you read has a BOM character in the beginning. Unfortunately, Node.js keeps it by default (see nodejs/node-v0.x-archive#1918). Then, GBK encoding has no code for the BOM character, so iconv-lite replaces it by '?'.

Solutions:

  1. Add data = data.replace(/^\uFEFF/, ''); before encoding.
  2. Use 'strip-bom' module for more readability.

Let me know if it helps.

@jincdream
Copy link
Author

"version": "0.4.4","GBK encoding has no code for the BOM character";
It's helpful , thank u.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants