Last active
December 17, 2015 22:39
-
-
Save ohgyun/5683254 to your computer and use it in GitHub Desktop.
Convert hex strings to binary strings.
This file contains hidden or 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
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