确定字节序的最Python方式是什么? [英] What's the most Pythonic way of determining endianness?

查看:99
本文介绍了确定字节序的最Python方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出计算代码运行的机器是大端还是小端的最佳方法。我有一个可行的解决方案(尽管我尚未在大端机上对其进行测试),但似乎有些笨拙:

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'

当然,根据您的计算机,它可能会返回。当然,您的方法也应该可以使用。

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

这篇关于确定字节序的最Python方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆