Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hex string to character in python

Tags:

python

hex

I have a hex string like:

data = "437c2123"

I want to convert this string to a sequence of characters according to the ASCII table. The result should be like:

data_con = "C|!#"

Can anyone tell me how to do this?

like image 559
Alice Avatar asked May 16 '12 12:05

Alice


People also ask

How do I read hex data in Python?

hex() function in Python hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.

What is a hex string in Python?

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.


1 Answers

In Python2

>>> "437c2123".decode('hex')
'C|!#'

In Python3 (also works in Python2, for <2.6 you can't have the b prefixing the string)

>>> import binascii
>>> binascii.unhexlify(b"437c2123")
b'C|!#'
like image 182
John La Rooy Avatar answered Sep 19 '22 16:09

John La Rooy