some device returns data (hex bytes) in the form of
data = b'\x07\x89\x00\x00\x12\x34'
How can I convert this to something in the form of the following?
['0x0789', '0x0000', '0x1234']
I already tried compositions of hexlify. I am using Python 3.5.
Take groups of two from your bytes
object. Multiply the first value from each group with 16**2. Add the two values. Use hex
on the result to convert it to its string representation.
>>> [hex(data[i]*16**2 + data[i+1]) for i in range(0,len(data),2)]
['0x789', '0x0', '0x1234']
I assume that you don't need your strings padded with useless zeros for now.
Use the struct module; it has unpack
function which allows to specify the chunk size size (byte, 2-byte, 4-bytes) and endianess in the data. If what you have is big-endian half-word sized data chunks, then the right format key is ">H".
To parse all data at one, add count in the format specifier: for example ">3H" for you input array. You can also write the number of fields dynamically.
Full example:
import struct
data = b'\x07\x89\x00\x00\x12\x34'
d = struct.unpack(">{}H".format(len(data) // 2), data) # extract fields
result = [hex(x) for x in d] # convert to strings
There are two steps:
You could use array
module from stdlib :
import sys
from array import array
a = array('H', data)
if sys.byteorder != 'big':
a.byteswap() # use big-endian order
result = ['0x%04X' % i for i in a]
# -> ['0x0789', '0x0000', '0x1234']
It is efficient, especially if you need to read data from a file .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.