0

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?

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.