简体   繁体   English

如何获得numpy dtype的字节序

[英]How to get endianness of numpy dtype

Related to Determine the endianness of a numpy array 确定numpy数组的字节顺序相关

Given an array 给定一个数组

x = np.arange(3)

I can get the byte order by doing 我可以通过这样做获得字节顺序

>>> x.dtype.byteorder
'='

How do I find out if this is big or little endian? 如何判断这是大端还是小端? I would like to get '<' , '>' or '|' 我想得到'<''>''|' as the output, not '=' . 作为输出,而不是'='

To be clear, I am not hung up on what format the information comes in. I just want to know "big endian", "little endian" or "irrelevant", but I don't care if it's "native" or not. 为了清楚起见,我不会挂断信息的格式。我只想知道“大端”,“小端”或“无关”,但我不在乎它是否是“原生”。

Probably just check sys.byteorder . 可能只需检查sys.byteorder Even the numpy.dtype.byteorder examples in the docs use sys.byteorder to determine what's native. 甚至文档中的numpy.dtype.byteorder示例也使用sys.byteorder来确定本机的内容。

endianness_map = {
    '>': 'big',
    '<': 'little',
    '=': sys.byteorder,
    '|': 'not applicable',
}

You can swap the endianness twice to make numpy reveal the true endianness: 你可以交换两次endianness使numpy显示真正的endianness:

dtype_nonnative = dtype.newbyteorder('S').newbyteorder('S')
dtype_nonnative.byteorder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM