Writing bytes to stdout reverses the bytes

Cameron Simpson cs at cskk.id.au
Sun Aug 19 20:51:55 EDT 2018


On 20Aug2018 00:31, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>When I write bytes to stdout, why are they reversed?
>
>[steve at ando ~]$ python2.7 -c "print('\xfd\x84\x04\x08')" | hexdump
>0000000 84fd 0804 000a
>0000005
>
>[steve at ando ~]$ python3.5 -c "import sys; sys.stdout.buffer.write(b'\xfd
>\x84\x04\x08\n')" | hexdump
>0000000 84fd 0804 000a
>0000005

They're not. Hexdump is presenting your bytes as little endian 2 byte words. So 
your leading 0xfd is the _low_ bytes of the 2 bytes word.

Try "od -c" instead.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list