flip bytes using Python's struct module
Question: I would like to flip from big to little endian this string:

`\x00\x40`

to have it like this:

`\x40\x00`

I guess the proper function to use would be struct.pack, but I can't find a
way to make it properly work. A small help would be very appreciated !

Thanks


Answer: You're not showing the whole code, so the simplest solution would be:

    
    
    data = data[1] + data[0]
    

If you insist on using `struct`:

    
    
    >>> from struct import pack, unpack
    >>> unpack('<H', '\x12\x13')
    (4882,)
    >>> pack('>H', *unpack('<H', '\x12\x13'))
    '\x13\x12'
    

Which first unpacks the string as a little-endian unsigned short, and then
packs it back as big-endian unsigned short. You can have it the other way
around, of course. When converting between BE and LE it doesn't matter which
way you're converting - the conversion function is bi-directional.

