2 ms·
You certainly can do this in python and often will need to when dealing with binary formats. The struct library is a great help for this. (https://docs.python.o
by seiferteric 10y ago
You certainly can do this in python and often will need to when dealing with binary formats. The struct library is a great help for this. (https://docs.python.org/2/library/struct.html https://docs.python.org/2/library/struct.html)
- Walkman 10y ago> This module performs conversions between Python values and C structs represented as Python strings. You can, but probably it's even slower than doing regular bit shifting operations (<<, >>, &, |, ^, ~)
- deathanatos 10y agoWhere you can, you should use struct. For example, decoding a 32-bit integer in struct is faster than doing it manually: In [11]: %timeit hex(b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)) The slowest run took 6.89 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 715 ns per loop In [12]: %timeit hex(struct.unpack('<I', b)[0]) The slowest run took 8.77 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 587 ns per loop The resulting code is also going to be cleaner, especially if you end up needing to unpack multiple members at once, which is common when parsing a binary file. The struct module is, I believe, written in C. (Pedantically, the struct module isn't; the C module _struct is hidden/private, but if you open the source for struct.py, you'll see it referenced.)
- deathanatos 10y agoThe struct library is great for decoding binary file formats, but it has no facilities for sub-byte packing such as what the article talks about. But if you have a structure composed of little or big-ending encoded 8-/16-/32-/64-bit signed/unsigned integers in a file (quite common) … struct is amazing.