struct.pack and struct.unpack only doing the first character of a string

589 Views Asked by At

So I'm trying to pack a packet header, and things are working fine, except for header flags I need to pack as strings are only unpacking the first character of the string.

For instance,

string = "ahhhhhh"
buffer = pack("s", string.encode('UTF-8'))
list = unpack("s", buffer)
print(list)

gives me (b'a')

What am I doing wrong?

1

There are 1 best solutions below

0
On

Ah, so for my format string, I have to specify the amount of characters in the string I'm packing

string = "ahhhhhh"
buffer = pack("7s", string.encode('UTF-8'))
list = unpack("7s", buffer)
print(list)

would be correct