Home > Back-end >  Convert hex string to ArrayBuffer
Convert hex string to ArrayBuffer

Time:08-25

I have image data in the form of a hex string

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, ...

So each value is separated by a , and space

How can I convert this into an ArrayBuffer object?

Thanks!

CodePudding user response:

One simpler option is to use teh package hex-to-array-buffer on npm where you can simply use teh function hexToArrayBuffer and convert the hex string to an array buffer.

You can also do it by yourself by doing:

var hex = your hex
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
  return parseInt(h, 16)
}))
var buffer = typedArray.buffer
  • Related