# Add middle-endian format to struct module

**URL:** <https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978>\
**Category:** Ideas\
**Created:** [December 16, 2022, 6:53pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978 "2022-12-16T18:53:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DamirHanov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/damirhanov/32/12200_2.png) [@DamirHanov](https://discuss.python.org/u/DamirHanov)\
**Post date:** [December 16, 2022, 6:53pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/1 "2022-12-16T18:53:26Z")

</div>

In my current project I need to parse binary data from different types of machines. Some of them may be very specific and use non standard endianness such as middle endian.

## Short example

If we have bytes A B C D then we have following orderings:

- Big endian: ABCD
- Little endian: DCBA
- Mid Big endian: BADC
- Mid Little endian: CDAB

## Large example

We have some uint\_32 encoded as bytes 0x19AA41AF.

- In **big-endian** it would be intepreted as 430588335. For this we convert 19 AA 41 AF as uint\_32 and that’s it.
- For **little-endian** case we need first to flip bytes, get AF 41 AA 19, and then interprete them as uint\_32. The result will be 2940316185.

In case of mid-endian we need to split our bytes on 2 halves 19AA 41AF, swap them and only then use rules of little or big endian converts.

- For **mid-big-endian** we swap bytes in halves. Get AA19AF41 and then convert.
- For **mid-little-endian** we swap halves.

Get 41AF19AA. Results are 2853809985 and 1101994410 respectively.

## Proposal

To add “native” support for such formats in struct module two new format specifier can be added. For example ^ and $ for mid-big and mid-little formats.  
I’m starting to work on C extension for my project and can implement this feature in struct.

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [December 16, 2022, 7:10pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/2 "2022-12-16T19:10:56Z")

</div>

I appreciate that you need this but it seems too obscure to add support to the struct module. Andi the proposed characters seem very non-mnemonic.

I think you’re better off just taking the 16-bit halves and combining them yousroin a small wrapper.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [December 16, 2022, 8:31pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/3 "2022-12-16T20:31:46Z")

</div>

I’m curious what computers have implemented mid-endian?  
Would they be very old 16-bit CPUs by any chance? Or is this custom silicon?  
Bytes in a word in one order and the words in memory in the other?

---

<div class="post-metadata">

**Author:** ![daniele](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daniele](https://discuss.python.org/u/daniele)\
**Post date:** [December 16, 2022, 8:51pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/4 "2022-12-16T20:51:28Z")

</div>

The only place where I’ve seen this is the modbus transport protocol: IIRC the “native” modbus register size in 16 bits wide and multiple consecutive registers are aggregated to create wider values, however there is no protocol or standard implementation for this and funky things are found in the wild, probably as result of some implementations not paying too much attention to endianes (when you code server and client at the same time, you can get away with getting endianes wrong, as long as you are consistent with your mistakes).

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [December 17, 2022, 9:45pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/5 "2022-12-17T21:45:56Z")

</div>

I agree with the implicit suggestions of Guido and Barry, that struct should handle 16-bit words and higher software handle word pairs. modbus is, multiple sites say, widely used to commucate between electronic devices. A [PyPI modbus search](https://pypi.org/search/?q=modbus) returns 193 projects, including one intended for generic use on [micropython](https://pypi.org/project/micropython-modbus/). I suggest looking at how any of these projects have dealt with the ‘middle-endian’ issue.

---

<div class="post-metadata">

**Author:** ![johnMt](https://avatars.discourse-cdn.com/v4/letter/j/dfb087/32.png) [@johnMt](https://discuss.python.org/u/johnMt)\
**Post date:** [December 20, 2022, 4:32pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/6 "2022-12-20T16:32:42Z")

</div>

Facing a same problem regarding the module structure but now hopefully it will be resolved  
[![sofa cum bed with storage](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/2/2db320be3c03934a314aa52895affcae6592013c.png)](https://thefurniturevilla.co.uk/sofa-cum-bed-uk/)

---

<div class="post-metadata">

**Author:** ![DamirHanov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/damirhanov/32/12200_2.png) [@DamirHanov](https://discuss.python.org/u/DamirHanov)\
**Post date:** [December 20, 2022, 5:30pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/7 "2022-12-20T17:30:30Z")

</div>

Thank you all for your suggestions. As for now we decided to stick to flipping necessary bytes and then parsing it by struct module.  
As Terry said, I’ve looked at some libs for parsing modbus. It seems that those libraries, that i’ve seen doesn’t implement this feature. I had hopes for [Construct](https://construct.readthedocs.io/en/latest/index.html) project, but didn’t find any mentioning of middle endian support there either. I’m planning to suggest my help on this to them, as it is suitable place for this.

---

<div class="post-metadata">

**Author:** ![DamirHanov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/damirhanov/32/12200_2.png) [@DamirHanov](https://discuss.python.org/u/DamirHanov)\
**Post date:** [December 20, 2022, 5:32pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/8 "2022-12-20T17:32:28Z")

</div>

I can’t really tell you much because of NDA. It is some old industrial hardware. And as others supposed we faced some modbus data

---

<div class="post-metadata">

**Author:** ![daniele](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daniele](https://discuss.python.org/u/daniele)\
**Post date:** [December 20, 2022, 8:00pm UTC](https://discuss.python.org/t/add-middle-endian-format-to-struct-module/21978/9 "2022-12-20T20:00:53Z")

</div>

PyModbus and MinimalModbus are among the most used modbus libraries and both have support for funny endianes, see [pymodbus package — PyModbus 3.1.0 documentation](https://pymodbus.readthedocs.io/en/latest/source/library/pymodbus.html#pymodbus.payload.BinaryPayloadBuilder) and [API for MinimalModbus — MinimalModbus 2.0.1 documentation](https://minimalmodbus.readthedocs.io/en/stable/apiminimalmodbus.html#minimalmodbus.BYTEORDER_BIG_SWAP) for example.
