Reputation: 1843
I am trying to pack a string using struct.pack. I am able to see complete value if I use integer type but when I want to use string I only see one character.
struct.pack("<1L",0xabcdabcd)
'\xab\xcd\ab\cd'
struct.pack("<1s","overflow")
'o' prints just s. I wanted it to print full string: overflow.
Upvotes: 0
Views: 712
Reputation: 1414
In the format string ("<1s"
) you're passing to struct.pack
, the 1 denotes the maximum number of characters that field can store. (See the paragraph beginning "For the 's'
..." in the struct
documentation.) Since you're passing 1, it will only store the first character. You'll need to choose a length that will fit any string you want to store in the struct, and specify that. For example, to store the string "overflow" (8 characters) you could use "<8s"
:
>>> struct.pack("<8s", "overflow")
'overflow'
Upvotes: 2