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?
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|!#'
In [17]: data = "437c2123"
In [18]: ''.join(chr(int(data[i:i+2], 16)) for i in range(0, len(data), 2))
Out[18]: 'C|!#'
Here:
for i in range(0, len(data), 2) iterates over every second position in data: 0, 2, 4 etc.
data[i:i+2] looks at every pair of hex digits '43', '7c', etc.
chr(int(..., 16)) converts the pair of hex digits into the corresponding character.
''.join(...) merges the characters into a single string.
Since Python 2.6 you can use simple:
data_con = bytes.fromhex(data)
The ord function converts characters to numerical values and the chr function does the inverse. So to convert 97 to "a", do ord(97)
Related
I want to convert a hex string to utf-8
a = '0xb3d9'
to
동 (http://www.unicodemap.org/details/0xB3D9/index.html)
First, obtain the integer value from the string of a, noting that a is expressed in hexadecimal:
a_int = int(a, 16)
Next, convert this int to a character. In python 2 you need to use the unichr method to do this, because the chr method can only deal with ASCII characters:
a_chr = unichr(a_int)
Whereas in python 3 you can just use the chr method for any character:
a_chr = chr(a_int)
So, in python 3, the full command is:
a_chr = chr(int(a, 16))
I would like to convert
the hex value
a = 0x32
to a string d = 32
Bascially I have a numpy array
msg = np.array[2, 50]
I need to convert both values to hex --- [0x02 , 0x32]
then print the hex values as strings on GUI as 2, 32
hex() will give you hex string. Then discard first 2 characters
>>> hex(a)
'0x32'
>>> hex(a)[2:]
'32'
>>>
Looking at the link to duplicate, it seems numpy is giving you the L at the end since you are running 64bit linux. so do this:
hex(a)[2:-1]
I'm reading a wav audio file in Python using wave module. The readframe() function in this library returns frames as hex string. I want to remove \x of this string, but translate() function doesn't work as I want:
>>> input = wave.open(r"G:\Workspace\wav\1.wav",'r')
>>> input.readframes (1)
'\xff\x1f\x00\xe8'
>>> '\xff\x1f\x00\xe8'.translate(None,'\\x')
'\xff\x1f\x00\xe8'
>>> '\xff\x1f\x00\xe8'.translate(None,'\x')
ValueError: invalid \x escape
>>> '\xff\x1f\x00\xe8'.translate(None,r'\x')
'\xff\x1f\x00\xe8'
>>>
Any way I want divide the result values by 2 and then add \x again and generate a new wav file containing these new values. Does any one have any better idea?
What's wrong?
Indeed, you don't have backslashes in your string. So, that's why you can't remove them.
If you try to play with each hex character from this string (using ord() and len() functions - you'll see their real values. Besides, the length of your string is just 4, not 16.
You can play with several solutions to achieve your result:
'hex' encode:
'\xff\x1f\x00\xe8'.encode('hex')
'ff1f00e8'
Or use repr() function:
repr('\xff\x1f\x00\xe8').translate(None,r'\\x')
One way to do what you want is:
>>> s = '\xff\x1f\x00\xe8'
>>> ''.join('%02x' % ord(c) for c in s)
'ff1f00e8'
The reason why translate is not working is that what you are seeing is not the string itself, but its representation. In other words, \x is not contained in the string:
>>> '\\x' in '\xff\x1f\x00\xe8'
False
\xff, \x1f, \x00 and \xe8 are the hexadecimal representation of for characters (in fact, len(s) == 4, not 24).
Use the encode method:
>>> s = '\xff\x1f\x00\xe8'
>>> print s.encode("hex")
'ff1f00e8'
As this is a hexadecimal representation, encode with hex
>>> '\xff\x1f\x00\xe8'.encode('hex')
'ff1f00e8'
In Python, how can I represent an integer value (<256) as a string? For example:
i = 10
How can I create a string "s" that is one-byte long, and the byte has the value 10?
to clarify, I do not want a string "10". I want a string that its 1st (and only) byte has the value of 10.
by the way, I cannot create the string statically:
s = '\x0A'
because the value is not pre-defined. It is a dynamic number value.
You can use chr() function as:
>>> chr(60)
'<'
>>> chr(97)
'a'
>>> chr(67)
'C'
To convert back use ord() funtion as:
>>> ord('C')
67
In Python 2.x, you want:
s = chr(10)
In Python 3.x, strings are Unicode, so you want:
s = bytes([10])
why don't you just use chr?
chr(10)
Out[41]: '\n'
chr(255)
Out[42]: '\xff'
Found another answer using struct module working for Python 2:
import struct
struct.pack('B', i)
By hex-string, it is a regular string except every two characters represents some byte, which is mapped to some ASCII char.
So for example the string
abc
Would be represented as
979899
I am looking at the binascii module but don't really know how to take the hex-string and turn it back into the ascii string.
Which method can I use?
Note: I am starting with 979899 and want to convert it back to abc
You can use ord() to get the integer value of each character:
>>> map(ord, 'abc')
[97, 98, 99]
>>> ''.join(map(lambda c: str(ord(c)), 'asd'))
'979899'
>>> ''.join((str(ord(c)) for c in 'abc'))
'979899'
You don't need binascii to get the integer representation of a character in a string, all you need is the built in function ord().
s = 'abc'
print(''.join(map(lambda x:str(ord(x)),s))) # outputs "979899"
To get the string back from the hexadecimal number you can use
s=str(616263)
print "".join([chr(int(s[x:x+2], 16)) for x in range(0,len(s),2)])
See http://ideone.com/dupgs