Skrey
Skrey

Reputation: 138

nodejs log buffer

Let's say I have a buffer constructed like this:

var buf = new Buffer(3);
buf.writeUInt16BE(258);
buf.write("a", 2);

and I only have the variable buf (no information about what's inside), how can I get something like this

[
{"value": 258, "type": "UInt16BE"},
{"value": "a", "type": "text"},
]

so I can console.log the content of the buffer?

Upvotes: 0

Views: 132

Answers (1)

manikawnth
manikawnth

Reputation: 3249

It is not possible if you construct the buffer like you did in your post.
Buffer is nothing but a byte array. It doesn't store any type information.

Buffer is not the right choice for you. If you want that way, you have to implement it yourself. This is called Object/data serialization where type and values are stored (Try checking for for msgpack or bson or json) . Buffer is just a byte stream.

NOTE: I'm not the one who down-voted your question. I believe that no question is trivial and not worth answering

Upvotes: 1

Related Questions