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.
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().
User contributions licensed under CC BY-SA 3.0