Home > database >  How can I pack 16 bytes in python with struct?
How can I pack 16 bytes in python with struct?

Time:10-21

How can i pack 16 bytes by using struct.pack ?
I don't see in the man format for that.

res = struct.pack(">???", 1234123412341234)

CodePudding user response:

where are there 16 bytes in your question? You entered a single integer number. You are aware that for the 16 digits that represent this number in decimal would only be "16 bytes" if this was an encoded string, with each byte representing one ASCII char, right?

And if you have a byte-string (which is the same as an encoded string), it is already packed as 16 bytes: no need to do anything.

I mean:


In [8]: a = "1234123412341234".encode()

In [9]: type(a)
Out[9]: bytes

In [10]: len(a)
Out[10]: 16

In [11]: list(a)
Out[11]: [49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52]

In [12]: a[0]
Out[12]: 49

In [13]: chr(a[0])
Out[13]: '1'
  • Related