We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,063 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Convert HEX into Binary Data

Hi all,
I am trying to send Binary data using In python raw socketusing socket.send().
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 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".

3
Contributors
7
Replies
13 Hours
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
debasishgang7
Junior Poster in Training
95 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Use functions hexlify() and unhexlify() in module binascii for this

>>> from binascii import hexlify, unhexlify
>>> s = '\x01\x00\x12\x59'
>>> h = hexlify(s)
>>> print h
01001259
>>> assert unhexlify(h) == s
>>>
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

thanks a lot for your reply..But one thing i wanna know what abou converting "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00" hex in a format like this "\x01\x00\x12\x59"

debasishgang7
Junior Poster in Training
95 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

thanks a lot for your reply..But one thing i wanna know what abou converting "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00" hex in a format like this "\x01\x00\x12\x59"

Remove white space and use unhexlify()

s = "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"
ss = s.replace(" ", "")
binary = unhexlify(ss)
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

Thanks again.I think its working but i am getting some special characters The printed code does not look like this one..-->> "\x01\x00\x12\x59"..If the printed special chars are equivalent to binary data can i send that through socket like i have mentioned in the main post.like

from binascii import hexlify, unhexlify
import socket
s = "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"
ss = s.replace(" ", "")
binary = unhexlify(ss)
print binary
sock.send(binary)
debasishgang7
Junior Poster in Training
95 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Or do this ...

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

hs = ""
for b in s.split():
    hs += "\\x%02x" % int(b, 16)

print(s)
print(hs)

'''
04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00
\x04\xf8\x00\x50\x4f\x30\xfb\x47\x28\x62\xa7\x6d\x50\x02\x02\x00\xd2\x7f\x00\x00
'''
vegaseat
DaniWeb's Hypocrite
Moderator
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36

Thanks again.I think its working but i am getting some special characters The printed code does not look like this one..-->> "\x01\x00\x12\x59"..If the printed special chars are equivalent to binary data can i send that through socket like i have mentioned in the main post.like

from binascii import hexlify, unhexlify
import socket
s = "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"
ss = s.replace(" ", "")
binary = unhexlify(ss)
print binary
sock.send(binary)

To print binary strings, you should print their repr():

>>> print repr(binary)
'\x04\xf8\x00PO0\xfbG(b\xa7mP\x02\x02\x00\xd2\x7f\x00\x00'

Otherwise, yes, you can send your binary string in the socket; hexlify() and unhexlify() are robust functions.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

Thnx a lot buddy..!!

debasishgang7
Junior Poster in Training
95 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Gribouillis and vegaseat

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0740 seconds using 2.66MB