Python Forum

Full Version: python3 convert hex to binary 32 bit
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
data_in_hex64digits='d0e66bfafaf5253157d1b5464e34b0d8d1b53bafc88ae4b7adb0f62d6812c24c'

data_out_bin32bit = 'đŠk ̇ ̇§%1WĐÁFN4░ěĐÁ;»╚ŐńĚş░÷-h↕┬L'

I need to convert a string hex 64 digits to a 32 bit binary string.

I tried different code but it didn't get me the desired result. Only result like '01010...'

Thanks in advance for the hint
What do you mean with 32 bit binary?

import binascii


result = binascii.unhexlify("d0e66bfafaf5253157d1b5464e34b0d8d1b53bafc88ae4b7adb0f62d6812c24c")
print(result)
Output:
b'\xd0\xe6k\xfa\xfa\xf5%1W\xd1\xb5FN4\xb0\xd8\xd1\xb5;\xaf\xc8\x8a\xe4\xb7\xad\xb0\xf6-h\x12\xc2L'
Read the docs of binascii.
I have code like this in php, but I don't quite understand what's going on.
salt='d0e66bfafaf5253157d1b5464e34b0d8d1b53bafc88ae4b7adb0f62d6812c24c'
$hexLenght = strlen($salt);
$saltBin = "";
for ($x = 1; $x <= $hexLenght/2; $x++) {
$saltBin .= (pack("H*", substr($salt,2 * $x - 2,2)));
}

Excuse me. Convert to a 32 byte binary string. This is what he writes in the documentation, and I do not fully understand what is going on. The documentation is in Polish
(Aug-27-2020, 01:15 PM)DeaD_EyE Wrote: [ -> ]What do you mean with 32 bit binary?

import binascii


result = binascii.unhexlify("d0e66bfafaf5253157d1b5464e34b0d8d1b53bafc88ae4b7adb0f62d6812c24c")
print(result)
Output:
b'\xd0\xe6k\xfa\xfa\xf5%1W\xd1\xb5FN4\xb0\xd8\xd1\xb5;\xaf\xc8\x8a\xe4\xb7\xad\xb0\xf6-h\x12\xc2L'
Read the docs of binascii.

Thank you very much, this is the solution.
I was misled by the data in the documentation.