patternpythonMinor
Converting a bytearray into an integer
Viewed 0 times
convertingbytearrayintegerinto
Problem
I am working on a program to decode MMS PDU files. They are binary files and I am reading them byte by byte and decoding each header value as I come to it.
One header value is the Date, and it's represented as follows (in hex):
I have code to read those 4 bytes and return it to me as a
Apparently
In order to parse this as a date, I need to convert it to an int. These bytes represent the timestamp
I have code to convert this byte array into an int, but there has got be a better way to do it than the way I came up with.
That is, I am converting every byte to a string (like
I then realized that I could replace the
Like I said, this code works, but I just feel there's a better way to do this than to convert the bytearray into a string and then parse that string back as a base-16 int.
One header value is the Date, and it's represented as follows (in hex):
85 04 57 E2 A2 49
0x85 is the "Date" header, 0x04 is the data length (4 bytes) and the 4 following bytes represent the date (timestamp).I have code to read those 4 bytes and return it to me as a
bytearray:bytearray(b'W\xe2\xa2I')Apparently
\x57 is a W and \x49 is an I.In order to parse this as a date, I need to convert it to an int. These bytes represent the timestamp
1474470473 (or 0x57E2A249).I have code to convert this byte array into an int, but there has got be a better way to do it than the way I came up with.
timestamp = int(''.join(map(hex, byte_range)).replace('0x', ''), 16)
value = datetime.fromtimestamp(timestamp)That is, I am converting every byte to a string (like
"0x57"), joining them together, removing the "0x"s, then reading that string as a base 16 value. This works, but it seems a little too convoluted.I then realized that I could replace the
join/map with binascii.hexlify, so now my code is:timestamp = int(binascii.hexlify(byte_range), 16)
value = datetime.fromtimestamp(timestamp)Like I said, this code works, but I just feel there's a better way to do this than to convert the bytearray into a string and then parse that string back as a base-16 int.
Solution
To decode binary data, use
struct.unpack().>>> import struct
>>> byte_range = bytearray(b'\x85W\xe2\xa2I')
>>> date_header, timestamp = struct.unpack('>BL', byte_range)
>>> date_header
133
>>> timestamp
1474470473Code Snippets
>>> import struct
>>> byte_range = bytearray(b'\x85W\xe2\xa2I')
>>> date_header, timestamp = struct.unpack('>BL', byte_range)
>>> date_header
133
>>> timestamp
1474470473Context
StackExchange Code Review Q#142915, answer score: 7
Revisions (0)
No revisions yet.