如何在JavaScript中将十六进制数据字符串转换为ArrayBuffer?

时间:2017-03-31 02:11:13

标签: javascript hex arraybuffer

如何将字符串'AA5504B10000B5'转换为ArrayBuffer

1 个答案:

答案 0 :(得分:8)

您可以将正则表达式与Array#mapparseInt(string, radix)一起使用:



var hex = 'AA5504B10000B5'

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

console.log(typedArray)
console.log([0xAA, 0x55, 0x04, 0xB1, 0x00, 0x00, 0xB5])

var buffer = typedArray.buffer