开发者

How to convert HEX data into BINARY data?

开发者 https://www.devze.com 2023-03-20 05:56 出处:网络
I am trying to send Binary data using python raw socket. For that i will do following. s = \'\\x01\\x00\\x12\\x59\' # some binary data

I am trying to send Binary data using python raw socket. For that i will do following.

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

i have created a DATAGRAM in HEX in by开发者_如何学Go sniffing a network traffic with wireshark.Which i want to send over the network.This hand made datagram is like

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

So i wanna convert this above mentioned HEX datagram into the binary format like "\x01\x00\x12\x59". How can i do that?


"04 f8 00 50".replace(' ', '').decode('hex')


Try 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()))


unhexlify is probably what you're looking for. It doesn't like whitespace, so for your example, unhexlify(data.replace(" ","")) should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消