0

I want to ask, how to store a number (integer in JS) in buffer using buffer.writeInt16BE().

Assume I have a number such as

var a = 40000; 

40000(10) = 9C40 (16)

how can I store 40000 in buffer of size of 2, so it looks something like:

<9c, 40>
tyrhus
  • 181
  • 1
  • 9

1 Answers1

1

You can't use buf.writeInt16BE() because 40000 is larger than 32768. However if you use buf.writeUInt16BE() you can write 40000 to the buffer just fine.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • I am getting an error like: ReferenceError: `writeUInt16BE is not defined` – tyrhus May 14 '14 at 13:47
  • 1
    Those are [methods](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_readuint16be_offset_noassert) available on the Buffer instance, not global functions. – mscdex May 14 '14 at 14:38