I have an array of signed ints that I want to interpret as binary data, therefore I did some Googling and found this short loop to iterate the array and produce the desired output:
import struct
data = [-24, -4, -19, 100,...,98, 0]
unpacked = ""
for d in data:
unpacked += struct.pack("i", d)[0]
print unpacked
From reading the man page of struct I understand that this is interpreting the data in the format "i" which equates to ints. What is the [0] doing in this code?
Also, how do I output the result in hex bytes?
Thanks
-
Are these supposed to be 4-byte ints, chars, or what? Also, you'll have to be more specific than "output the result in hex bytes". Python has no specific type for hex. If you want to print it in hex, you'll have to be a bit more specific about the output format; if you want to do something inside Python in hex format, you've probably gotten confused about what you actually need.user2357112– user23571122017年07月18日 18:54:22 +00:00Commented Jul 18, 2017 at 18:54
-
yes, printing in hex to the console is what i meant and also 4-byte ints, per the below answer.user2672288– user26722882017年07月18日 20:53:14 +00:00Commented Jul 18, 2017 at 20:53
2 Answers 2
I believe that's a bug. struct.pack() returns the packed bytes. Since the format asks for 32-bit integer, it returns 4 bytes. That [0] takes the first byte of those. But if the packed number is outside of the 8-bit range, it'll be truncated. You should instead use b format which is for signed char. That way you're going to get a proper exception if the number is out of range.
>>> struct.pack("b", 1000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: byte format requires -128 <= number <= 127
So the way I would code this is:
import struct
data = [-24, -4, -19, 100,...,98, 0]
unpacked = ""
for d in data:
unpacked += struct.pack("b", d)
print unpacked
2 Comments
unpack that requires [0] when processing single values. Probably we have to blame the misleading name of the accumulator (unpacked)The struct package generates a binary string for the integer. The i denotes a four byte integer. It will thus return a bytestring with four bytes.
The [0] means that you want to obtain the first byte. If the machines with little endianness this means that the least significant byte will be obtained. I guess the designers of the algorithm were after this. It thus means that if the data is within the 0-255 range, you obtain a bytestring with one byte of that data.
In case the machine is big endianness however. One will obtain the most significant bit. This will for your example be 255 for negative values, and 0 for positive ones, since all values in the example seem to be in the -255 - 255 range.