0

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

asked Jul 18, 2017 at 18:49
2
  • 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. Commented 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. Commented Jul 18, 2017 at 20:53

2 Answers 2

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
answered Jul 18, 2017 at 18:56
Sign up to request clarification or add additional context in comments.

2 Comments

It's surely a bug. It's unpack that requires [0] when processing single values. Probably we have to blame the misleading name of the accumulator (unpacked)
awesome, it didn't make sense to me either in the sense of pack instead of unpack. pleased to know it is a bug and your code above is actually exactly what im after. thanks!
0

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.

answered Jul 18, 2017 at 18:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.