Skip to content

Instantly share code, notes, and snippets.

@themikefuller
Created October 15, 2018 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themikefuller/608202bde24077990c0539f960b79fe4 to your computer and use it in GitHub Desktop.
Save themikefuller/608202bde24077990c0539f960b79fe4 to your computer and use it in GitHub Desktop.
Encoding and decoding strings into hex encoding.
function string2hex(text) {
let encoded = new TextEncoder().encode(text);
let hex = Array.from(encoded).map(val=>{
return ('00' + val.toString(16)).slice(-2);
}).join('');
return hex;
}
function hex2string(hex) {
let byteArray = new Uint8Array(hex.match(/.{0,2}/g).map(val=>{
return parseInt(val,16);
}));
return new TextDecoder().decode(byteArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment