Skip to content

Instantly share code, notes, and snippets.

@ICeZer0
Created January 18, 2019 12:06
Show Gist options
  • Save ICeZer0/f72b8cab4737c021e33414bd1fc39cc0 to your computer and use it in GitHub Desktop.
Save ICeZer0/f72b8cab4737c021e33414bd1fc39cc0 to your computer and use it in GitHub Desktop.
Converts 4 byte Hex to 32 bit binary string
function Hex32BitToBinary(HexData){
let result = "";
//32bit hex to string
HexData.match(/.{1,2}/g).forEach(str => {
result += hex2bin(str)
});
return result;
}
//Function to convert hex string to binary
function hex2bin(hex) {
let regEx = /^[0-9]*$/g;
let binary = ("00000000" + (parseInt(hex, 16)).toString(2)).substr(-8);
if (regEx.test(binary)) {
return binary;
} else {
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment