although Buffer's 'binary' is deprecated, I have to use it: I write a web app use node js +express. user can download file, and filename will Garbled if not use
res.download(allpath,buf0.toString('binary'));
watch the value (is a chinese char):
console.log(new Buffer('牛'));
output: Buffer,e7,89,9b
and
var buf0=new Buffer('牛');
console.log(new Buffer(buf0.toString('binary')));
output: Buffer,c3,a7,c2,89,c2,9b
what meaning this algorithm,and why use binary toString is work?
Actually, new Buffer('牛')
is a shortcut for new Buffer('牛', 'utf-8')
.
Thus if you want to convert it back to a string you have to use toString('utf-8')
.
Example:
console.log(new Buffer('牛'));
// Output: <Buffer e7 89 9b>
var buf0=new Buffer('牛');
console.log(new Buffer(buf0.toString('utf-8')));
// Output: <Buffer e7 89 9b>
Further reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With