Node js Buffer.toString ('binary')

although the Buffer 'binary' is deprecated, I have to use it: I am writing a web application using node js + express. the user can upload the file, and the file name will be Garbled if not use

res.download(allpath,buf0.toString('binary')); 

look up the value (this is a Chinese char):

 console.log(new Buffer('牛')); 

: Buffer, e7.89.9b

and

 var buf0=new Buffer('牛'); console.log(new Buffer(buf0.toString('binary'))); 

: Buffer, c3, a7, c2.89, c2.9b

what does this algorithm mean, and why use a binary toString?

+6
source share
1 answer

In fact, new Buffer('牛') is a shortcut for new Buffer('牛', 'utf-8') .
Thus, if you want to convert it back to a string, you should 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:

+3
source

Source: https://habr.com/ru/post/943697/


All Articles