Reputation: 323
Im trying to convert my very long integer list i a binary array (list) Im using struct.pack() and it works well but i think this is very ugly:
buf = pack(">IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII", *data)
I need to do this because my list has 113 Items with large and small values. Is there a option to use pack() with a long list without having 113 "I"'s?
Upvotes: 1
Views: 435
Reputation: 476719
If you multiply a string with an integer, the string is repeated that many times. For instance:
>>> 'foo'*3
'foofoofoo'
So you can use:
buf = pack(">" + "I"*len(data), *data)
This is more elegant and safe as well: in case the number of objects changes, this will simply keep working.
Upvotes: 4