JavaScript number to 32 bit int equivalent

0

I would like to "convert" a number in JS to its C style int equivalent. For example: 259 -> 0x00000103.

More specifically I need to send an int as a packet across a socket to a C++ application. So on C side I can simply do a C style cast like: (unsigned int) packet[0]; on the packet.

javascript
node.js
asked on Stack Overflow May 21, 2014 by Zinglish

1 Answer

3

Assuming you are sending it via a plain TCP socket, you will probably be using Buffers when you write to the socket. Buffer has a writeInt32BE/LE() method that you can use to write a signed 32-bit integer to a Buffer.

If you need to write the number as an unsigned 32-bit integer instead, you can use writeUInt32BE/LE().

answered on Stack Overflow May 21, 2014 by mscdex

User contributions licensed under CC BY-SA 3.0