Created
June 27, 2013 10:29
-
-
Save valentinkostadinov/5875467 to your computer and use it in GitHub Desktop.
JavaScript HEX encoding
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
function toHex(s) { | |
// utf8 to latin1 | |
var s = unescape(encodeURIComponent(s)) | |
var h = '' | |
for (var i = 0; i < s.length; i++) { | |
h += s.charCodeAt(i).toString(16) | |
} | |
return h | |
} | |
function fromHex(h) { | |
var s = '' | |
for (var i = 0; i < h.length; i+=2) { | |
s += String.fromCharCode(parseInt(h.substr(i, 2), 16)) | |
} | |
return decodeURIComponent(escape(s)) | |
} |
MGF15
commented
Nov 19, 2022
@MGF15 that's only possible in NodeJS, this Gist is portable
Actually, this Gist is also not portable between environments. My bad
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment