Python 如何判断ctypes整数类型是否为big-endian?
考虑以下几点:Python 如何判断ctypes整数类型是否为big-endian?,python,ctypes,Python,Ctypes,考虑以下几点: le = ctypes.c_uint32.__ctype_le__ be = ctypes.c_uint16.__ctype_be__ 我如何编写一个函数is_bigendiancls,它的行为与您期望的一样: >>> is_bigendian(le) False >>> is_bigendian(be) True ctypes是否在某处公开了此信息?似乎唯一可能的方法是: if ctypes.c_uint8.__ctype_be__.__
le = ctypes.c_uint32.__ctype_le__
be = ctypes.c_uint16.__ctype_be__
我如何编写一个函数is_bigendiancls,它的行为与您期望的一样:
>>> is_bigendian(le)
False
>>> is_bigendian(be)
True
ctypes是否在某处公开了此信息?似乎唯一可能的方法是:
if ctypes.c_uint8.__ctype_be__.__name__.endswith('_be'):
def is_bigendian(cls):
return cls.__name__.endswith('_be')
elif ctypes.c_uint8.__ctype_le__.__name__.endswith('_le'):
def is_bigendian(cls):
return not cls.__name__.endswith('_le')
else:
raise RuntimeError
这很难识别,因为reprctypes.c_uint8.\uuuu ctype_be\uuuuuuuuu无法正确显示类名。无法区分它们,因此您可能会被忽略。