Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node js 's Buffer.toString( 'binary')

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?

like image 538
user1625647 Avatar asked Apr 26 '13 11:04

user1625647


1 Answers

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:

  • How to Use Buffers in Node.js
like image 172
fsenart Avatar answered Oct 16 '22 04:10

fsenart