Writing binary data to Buffer

357
January 19, 2017, at 03:54 AM

Normally, I would expect that the following would be good enough to represent binary data in a Buffer:

new Buffer('01001000','binary')

but I am pretty certain Node.js/JS does not support this 'binary' encoding. What is the best way then to write binary data to a buffer?

Answer 1

You can do binary encoding like this:

 var binaryString = "\xff\xfa\xc3\x4e";
 var buffer = new Buffer(binaryString, "binary");
 console.log(buffer);
 <Buffer ff fa c3 4e>

//types of encoding allowed

encoding            size (bytes) 
base64                 4,177,241 
binary                   4,162,398 
hex                       4,669,965 
JSON                   2,271,670 
utf16le*                4,543,605 
utf8*                     3,640,132 
ascii*                    2,929,850 
READ ALSO
Error:Can&#39;t set headers after they are sent in express

Error:Can't set headers after they are sent in express

I am exploring node and express with redux where I want to set a cookie after the page has been rendered and wanna use the updated state for setting a cookie where I am getting this error

510
Nodejs - Mocha, Chai: How to test an end point that passes control to multiple middleware functions by calling next

Nodejs - Mocha, Chai: How to test an end point that passes control to multiple middleware functions by calling next

I have an end point that passes control to multiple middleware functions by calling next() for example:

300
set custom headers dynamically in express app to be accessible by angular app

set custom headers dynamically in express app to be accessible by angular app

i am quiet new to expressI have searched a lot and unable to find the answer

358
mongoose model method - require other models

mongoose model method - require other models

I have 2 models ModelA and ModelB, both require the other oneMy problem is that if I put require on the top of my model when I try to do a simple query like ModelB

291