Created
July 6, 2022 03:35
-
-
Save mjschutz/244627bcf250da07787646ab2a7430ea to your computer and use it in GitHub Desktop.
This file contains 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
// https://stackoverflow.com/a/60505243/4952028 | |
function hexToUtf8(s) | |
{ | |
return decodeURIComponent( | |
s.replace(/\s+/g, '') // remove spaces | |
.replace(/[0-9a-f]{2}/g, '%$&') // add '%' before each 2 characters | |
); | |
} | |
const utf8encoder = new TextEncoder(); | |
function utf8ToHex(s) | |
{ | |
const rb = utf8encoder.encode(s); | |
let r = ''; | |
for (const b of rb) { | |
r += ('0' + b.toString(16)).slice(-2); | |
} | |
return r; | |
} | |
var hex = "d7a452656c6179204f4e214f706572617465642062792030353232"; | |
var utf8 = hexToUtf8(hex); | |
var hex2 = utf8ToHex(utf8); | |
console.log("Hex: " + hex); | |
console.log("UTF8: " + utf8); | |
console.log("Hex2: " + hex2); | |
console.log("Is conversion OK: " + (hex == hex2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment