What's the most Pythonic way of determining endianness?

痴心易碎 提交于 2019-12-20 10:59:39

问题


I'm trying to find the best way of working out whether the machine my code is running on is big-endian or little-endian. I have a solution that works (although I haven't tested it on a big-endian machine) but it seems a bit clunky:

import struct
little_endian = (struct.pack('@h', 1) == struct.pack('<h', 1))

This is just comparing a 'native' two-byte pack to a little-endian pack. Is there a prettier way?


回答1:


The answer is in the sys module:

>>> import sys
>>> sys.byteorder
'little'

Of course depending on your machine it may return 'big'. Your method should certainly work too though.



来源:https://stackoverflow.com/questions/1346034/whats-the-most-pythonic-way-of-determining-endianness

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!