Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:06
Show Gist options
  • Save st98/b1ac821a7f90b0b924a3 to your computer and use it in GitHub Desktop.
Save st98/b1ac821a7f90b0b924a3 to your computer and use it in GitHub Desktop.
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'
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