UNPKG

hex-to-array-buffer

Version:

Turn a string of hexadecimal characters into an `ArrayBuffer`.

18 lines (13 loc) 447 B
export default function hexToArrayBuffer (input) { if (typeof input !== 'string') { throw new TypeError('Expected input to be a string') } if ((input.length % 2) !== 0) { throw new RangeError('Expected string to be an even number of characters') } const view = new Uint8Array(input.length / 2) for (let i = 0; i < input.length; i += 2) { view[i / 2] = parseInt(input.substring(i, i + 2), 16) } return view.buffer }