Created
October 15, 2018 01:48
-
-
Save themikefuller/608202bde24077990c0539f960b79fe4 to your computer and use it in GitHub Desktop.
Encoding and decoding strings into hex encoding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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