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.

VIJ
  • 1,516
  • 1
  • 18
  • 34
  • Possible duplicate of [How to display nodejs raw Buffer data as Hex string](https://stackoverflow.com/questions/18879880/how-to-display-nodejs-raw-buffer-data-as-hex-string) – Aramil Rey Jul 30 '18 at 14:37

1 Answers1

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 ]
Daphoque
  • 4,421
  • 1
  • 20
  • 31