Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试找到确定我的代码运行的机器是大端还是小端的最佳方法。我有一个可行的解决方案(虽然我没有在大端机器上测试过),但它似乎有点笨拙:
import struct little_endian = (struct.pack('@h', 1) == struct.pack('<h', 1))
这只是将“本机”两字节包与小端包进行比较。有没有更漂亮的方法?
答案在sys 模块中:
>>> import sys >>> sys.byteorder 'little'
当然,根据您的机器,它可能会返回'big'。不过,您的方法当然也应该有效。
'big'