Daniel T. Lee Daniel T. Lee - 1 year ago 133
Node.js Question

(NodeJS) How Can I put UInt16 String directly to Buffer as writeUInt16BE?

As In Node JS buffer Like this, int 450 -> converts into 01C2 as below Image.

Buffer Input Example

I have some string with already Hex encoded String.

var a = "C2323DAD8D2D";

How Can I put this string into Buffer directly, as it looks like this?

[Buffer C2 32 3D AD 8D 2D]

Answer Source

The way I achieve this was by creating buffer with Array-like Object.

Difference between Array and Array-like object can be found at here.
Array vs. Array-like object

Buffer.from(object)

-> TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.

As the Error Says, Buffer can be created with those types.

var buf = {};
buf.length = 3;
buf[1] = "AB";
buf[2] = "CD";
buf[3] = "EF";

var Buf = Buffer.from(buf)

It works!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download