Returning binary data

25 views
Skip to first unread message

Mitchell Hockley

unread,
Oct 4, 2016, 1:22:57 AM10/4/16
to nodejs
Could anyone help me with the following please?

How can I return a stream of bits which represents an array of double precision numbers?

For example, if I wanted to return two values each equal to 0.3333333333333 I could do the following in nodejs:

app.get('/GetBinaryData', function(req, res)
                                   {
                                        var buf = new Buffer(16);
 
                                        buf[0] = 0x55;
                                        buf[1] = 0x55;
                                        buf[2] = 0x55;
                                        buf[3] = 0x55;
                                        buf[4] = 0x55;
                                        buf[5] = 0x55;
                                        buf[6] = 0xd5;
                                        buf[7] = 0x3f;

                                        buf[8] = 0x55;
                                        buf[9] = 0x55;
                                        buf[10] = 0x55;
                                        buf[11] = 0x55;
                                        buf[12] = 0x55;
                                        buf[13] = 0x55;
                                        buf[14] = 0xd5;
                                        buf[15] = 0x3f;

                                        res.send(buf);
                                   }
       );

At the client side I receive the stream and read 8 bits at a time to get the two values.

I understand there may be endian issues, but ignoring them the questions simplifies to:

If I want to send the content of this array:

var array = [7.1,5.2,13.3,300.4];

to a client from nodejs as binary data how can I do it?

(can be floats if necessary. Maybe there's a entirely better way I am unaware of, that would be great).

Thanks fro any help,
Mitch.

Sergey Jamy

unread,
Oct 6, 2016, 10:39:25 AM10/6/16
to nodejs

Zlatko

unread,
Oct 6, 2016, 10:39:54 AM10/6/16
to nodejs
Maybe Buffer.from(arrayBuffer) seems like it could help?

Basically a buffer from a typed array. Then stream that to client. 

Mitchell Hockley

unread,
Oct 6, 2016, 10:41:41 AM10/6/16
to nodejs
No worries, solved now.
Reply all
Reply to author
Forward
0 new messages