How to convert HEX data to BINARY data?

I am trying to send binary data using python raw socket. To do this, I will do the following.

s = '\x01\x00\x12\x59' # some binary data sock.send(s) # assuming "sock" is a valid, open socket object 

I created DATAGRAM in HEX, sniffing network traffic using wirehark. What I want to send over the network. This manual datagram is similar to

"04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"

So, I want to convert this aforementioned HEX datagram to binary format, like "\ x01 \ x00 \ x12 \ x59". How can i do this?

+4
source share
3 answers
 "04 f8 00 50".replace(' ', '').decode('hex') 
+2
source

Try using the following code:

 "".join("04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00".split()).decode('hex') 

OR

 import binascii print binascii.unhexlify("".join("04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00".split())) 
+4
source

unhexlify is probably what you are looking for. This doesn't like the space, so for your example, unhexlify(data.replace(" ","")) should work.

0
source

All Articles