Skip to content

Instantly share code, notes, and snippets.

@ohgyun
Last active December 17, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohgyun/5683254 to your computer and use it in GitHub Desktop.
Save ohgyun/5683254 to your computer and use it in GitHub Desktop.
Convert hex strings to binary strings.
var str = '0x48 0x65 0x6c 0x6c 0x6f'; // Hello
hexstr2binstr(str); //--> '0100 1000 0110 0101 0110 1100 0110 1100 0110 1111'
function hexstr2binstr(hexstr) {
return hexstr.split(' ').map(function (v) {
var bins = Number(v).toString(2).split('');
while (bins.length < 8) {
bins.unshift('0'); // left padding with '0'
}
bins.splice(4, 0, ' '); // add space at each 4 unit
return bins.join('');
}).join(' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment