Python Forum
python3 convert hex to binary 32 bit - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python3 convert hex to binary 32 bit (/thread-29298.html)



python3 convert hex to binary 32 bit - Mkt - Aug-27-2020

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


RE: python3 convert hex to binary 32 bit - DeaD_EyE - Aug-27-2020

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.


RE: python3 convert hex to binary 32 bit - Mkt - Aug-27-2020

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


RE: python3 convert hex to binary 32 bit - Mkt - Aug-28-2020

(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.