什么是确定排序的最Python方式?(What's the most Pythonic way of determining endianness?)
我试图找出计算我的代码运行的机器是大端还是小端的最佳方法。 我有一个可行的解决方案(虽然我没有在大型机器上测试过),但看起来有点笨拙:
import struct little_endian = (struct.pack('@h', 1) == struct.pack('<h', 1))这只是比较“本地”双字节包和小端包。 有更漂亮的方法吗?
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?
最满意答案
答案在sys模块中 :
>>> import sys >>> sys.byteorder 'little'当然,根据你的机器,它可能会返回'big' 。 你的方法当然也应该工作。
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.
更多推荐
发布评论