- function hexToBytes(hex) {
- hex = hex.replace(/ /g, ''); // strip spaces
- for (var bytes = [], c = 0; c < hex.length; c += 2)
- bytes.push(parseInt(hex.substr(c, 2), 16));
- return new Uint8Array(bytes);
- }
- var str = "486579";
- var byteArray = hexToBytes(str); // returns Uint8Array(4) [222, 173, 190, 239]
- var decoded = new TextDecoder("utf-8").decode(byteArray); // returns "Hey"