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