0

I have a byte array field in mongodb. while reading i have to show the binary data as hex string.

I am using express js, mongoose , node js to read data from mongodb.

In java to convert hex string to byte array we have:

byte[] bytes = DatatypeConverter.parseHexBinary(s);

And byte array to hex string we have

DatatypeConverter.printHexBinary(bytes);

I want to similar function which does the same job in node js.

also please guide me how to define schema to achieve in mongoose.

1

1 Answer 1

0

In node you can use Buffer:

var a = Buffer.from([0x06, 0x75, 0x66, 0x66, 0x65, 0x72]).toString("hex"); // '067566666572'

// reverse operation
var b = Array.prototype.slice.call(new Buffer.from(a, "hex"), 0); // [ 6, 117, 102, 102, 101, 114 ]
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.