4 ms·
Struct Endianness in Python
- qsort 4y agoI literally used this module in a project at my day job. We had to deal with geospatial data in WKB representation, a binary format that supports both endiannesses. Turns out deserializing that data as a struct is easier than dealing with wild bits *shrug*
- sgtnoodle 4y agoAs an embedded software engineer, I find myself using struct in pretty much every python script I ever write. When you're churning through a lot of data, instantiating a Struct object will give a nice speedup vs. passing in the format string on every pack/unpack.
- Dries007 4y agoAlso: If you don't specify a byte order character, the size of fields is platform dependant! Docs: https://docs.python.org/3/library/struct.html#format-characters https://docs.python.org/3/library/struct.html#format-charact...
- jamesfinlayson 4y agoI guess that makes sense - are there any programming languages that use a fixed endianness?
- idatum 4y ago> Turns out Python can tell you if you are big-endian or little-endian like this: sys.byteorder Cool. Handy way to do a quick check. I didn't realize Raspbian on Pi is little-endian. Most I've used struct is with XBee ZigBee protocol, which uses big-endian.
- vonwoodson 4y agoThe fact that Python let’s you pick byte order so easily has saved my skin bunches of times. Great article.
- jonathrg 4y agoOw, good to know. int.to_bytes and bytes.to_int requires a byteorder argument, curious that struct doesn't