user20007266
user20007266

Reputation: 87

How can I pack 16 bytes in python with struct?

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

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

Upvotes: 0

Views: 435

Answers (1)

jsbueno
jsbueno

Reputation: 110801

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'

Upvotes: 0

Related Questions