什么是确定排序的最Python方式?(What's the most Pythonic way of determining endianness?)

编程入门 行业动态 更新时间:2024-10-26 01:25:36
什么是确定排序的最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.

更多推荐

本文发布于:2023-07-20 12:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   Python   Pythonic   endianness   determining

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!