Python Forum
python3 convert hex to binary 32 bit
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python3 convert hex to binary 32 bit
#1
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
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert binary data into text? ZYSIA 3 2,585 Jul-16-2021, 04:18 PM
Last Post: deanhystad
  Convert file of hex strings to binary file medatib531 4 13,571 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  convert string into multiple columns in python3 VAN 2 2,723 Sep-26-2020, 11:14 PM
Last Post: scidam
  Python3 binary tree not replacing parent nodes with child nodes Aspect11 0 1,741 Sep-23-2020, 02:22 PM
Last Post: Aspect11
  Binary data to Image convert Nuwan16 1 5,562 Aug-24-2020, 06:03 AM
Last Post: millpond
  How to convert a python code to binary? rsurathu 0 1,770 Aug-02-2020, 08:09 AM
Last Post: rsurathu
  can anyone help convert to python3 corsasri 24 142,029 Jul-17-2020, 07:28 AM
Last Post: corsasri
  Python3: how to read binary? alexel 0 1,478 Feb-03-2020, 09:18 PM
Last Post: alexel
  hex file to binary or pcap to binary baran01 1 5,629 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,823 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020