Convert hex to ASCII string in Python

Sometimes web pages use hexadecimal values of of ASCII characters. To parse them we need to convert them to ASCII string first. Today I faced a similar problem and came up with the following solution:

import binascii, re

def asciirepl(match):
# replace the hexadecimal characters with ascii characters
s = match.group()
return binascii.unhexlify(s)

def reformat_content(data):
p = re.compile(r'\\x(\w{2})')
return p.sub(asciirepl, data)

hex_string = '\x70f=l\x26hl=en\x26geocode=\x26q\x3c'
ascii_string = reformat_content(hex_string)

print ascii_string


I took help of binascii module (and of course the re module). The logic was simple, I just replaced \x.. (here .. represents two characters) with their corresponding ASCII characters.

You can check my other post to convert ASCII values to ASCII characters: http://love-python.blogspot.com/2008/04/get-ascii-string-for-decimal-number-in.html

And please share if you have any other idea.

Comments

FreeZER said…
Hi...
I'm a newbie in python.
But I really need this hex to ascii and ascii to hex converter.
Could you please, give me example how to use your code here? And please write an article about ascii to hex converter too...
I'll greatly appreciate it...
Thank U
:D
FreeZER said…
Oh, and 1 more thing, could you please tell me how to generate hash code in python?
Thank U
:D
Tamim Shahriar said…
Ok. I shall write aascii to hex converter in future.

What kind of hash you are talking about. Once I created md5 hash using Python module md5.
FreeZER said…
Thanks for your attention, bro.
I'm totally new to programming languages. I learn vb6 and AutoHotKey before. Now I interested in python especially pys60. Because it can be done everywhere without having to bring my old laptop to do little code experiments.
Ok, cut that out, I don't wanna waste your time for my stories.

First, I want to make a chat application with pys60. Its called mig33 chat.
And to make it work, I have to build packets to be send to the server side and slice every packets that sent by the server.
The easy way to build the packet is converting the the ASCII codes to hex, add every codes then convert them back to ASCII before send it to the server.
And so when I receive the respon packet from the server.
I've tried your code above, but I dont get the result as I expected.
It's result like this:

(I tried to convert 0x61 to an "a")

pf=l&hl=en&geocode=&q<0x61

I put your code like this:

import binascii, re
hex_string = raw_input("hex number? " )
def asciirepl(match):
# replace the hexadecimal characters with ascii characters
s = match.group()
return binascii.unhexlify(s)
def reformat_content(data):
p = re.compile(r'\\x(\w{2})')
return p.sub(asciirepl, data)
hex_string = '\x70f=l\x26hl=en\x26geocode=\x26q\x3c' + hex_string
ascii_string = reformat_content(hex_string)
print ascii_string

please give me an the right example to use your code...
:)

and about that hash generator, I don't know what kind that I want.
The basic function it has is join a random string from the server with my password then it produces 4 byte hash to send it back to the server.
Where is that md5 modul you told me?
Maybe I can try to use it.

Too long for a comment, eh?
:P

I'm really greatly appreciate if you willing to teach me.
thanks in advance...

:D
Unknown said…
This is slightly broken ...
Notice that if you:

print hex_string

python has already parsed and decoded the hex values in the string.
What is needed is a raw string to start:

hex_string = r"\x70f=l\x26hl=en\x26geocode=\x26q\x3c"

I noticed the mistake after seeing that the function asciirepl() was never getting called ... because as far as python was concerned there were no \x?? strings in the hex_string since it was already interpreting them.
After I fixed that it broke the function asciirepl() because the unhexify(s) apparently only takes the two digits without the '\x' attached to it. I came up with this simple fix to only pass the two digits:

return binascii.unhexlify(s[2:])

After that it all worked beautifully.
Munna / Rakesh said…
hey thanks for the code snippet dude.
My Hex String is : '\x04\x00\x00\x00\x00\x06\xff\x03\x00\x01\x00\x02'

When I use the same code as urs , I get some weird characters as my o/p when I write the o/p to a file or when I try to print on the shell.

Please help

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code