Reputation: 11
When I try to pack an integer number, I'm getting an unexpected output. For values less than or equal to 16 the output is just as I expected, however, for values greater it breaks the output I expect. I've attached a few examples below.
Integer: 4
Output: b'\x00\x00\x00\x04'
Integer: 56
Output: b'\x00\x00\x008'
In this instance the integer 4 works just fine, however, I expected the output for 56 to be b'\x00\x00\x00\x38'
(converted to hex)
Integer: 20
Output: b'\x00\x00\x00\x14'
Integer: 56
Output: b'\x00\x00\x00('
Integer: 8
Output: b'\x00\x00\x00\x08'
I this instance the integer 8 gives me an expected output, however, for the integer 20 I expect b'\x00\x00\x00\x14'
and for a size of 40 I expect b'\x00\x00\x00\x28'
(This is one is especially confusing me because of the "("
).
I've tried 3 different ways so far to convert these integers however they all give me special characters for larger integers.
struct.pack('>i', (size)
bytes([size])
(int(size)).to_bytes(1, byteorder='big')
I suspect that I need to manipulate the integer before packing it someway, but I feel very stuck at the moment. Is there something wrong with what I assume should be the expected output? Or is there any hint you could give me to set me on the right path?
PS: This is my first post so apologies if I'm not being clear haha
Upvotes: 0
Views: 301
Reputation: 33179
For printable characters in the ASCII range, Python displays them as the characters themselves.
>>> b'\x38'
b'8'
>>> b'\x28'
b'('
In other words, you're confusing your data with its representation.
Upvotes: 2