Last active
August 29, 2015 14:06
-
-
Save st98/b1ac821a7f90b0b924a3 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
function _repeat(str, n) { | |
return Array(n + 1).join(str); | |
} | |
function _zfill(str, n) { | |
return (_repeat('0', n) + str).slice(-n); | |
} | |
function hex(str) { | |
return str.split('').map(function (c) { | |
return _zfill(c.charCodeAt(0).toString(16), 2); | |
}).join(''); | |
} | |
function unhex(str) { | |
return str.match(/[0-9A-Fa-f]{2}/g).map(function (c) { | |
return String.fromCharCode(parseInt(c, 16)); | |
}).join(''); | |
} | |
//----- | |
hex('hello'); // => '68656c6c6f' | |
unhex('68656c6c6f'); // => 'hello' |
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 unhexe32(str) { | |
return str.match(/[0-9A-Fa-f]{8}/g).map(function (m) { | |
return m.match(/[0-9A-Fa-f]{2}/g).reverse().map(function (c) { | |
return String.fromCharCode(parseInt(c, 16)); | |
}).join(''); | |
}).join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment