Skip to content

Instantly share code, notes, and snippets.

@postwait
Forked from valentinkostadinov/hex.js
Created October 30, 2017 19:35
Show Gist options
  • Save postwait/05b1731a181da76400c8f210f9d4579e to your computer and use it in GitHub Desktop.
Save postwait/05b1731a181da76400c8f210f9d4579e to your computer and use it in GitHub Desktop.
JavaScript HEX encoding
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment