简体   繁体   中英

How to get endianness of numpy dtype

Related to Determine the endianness of a numpy array

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 . Even the numpy.dtype.byteorder examples in the docs use sys.byteorder to determine what's native.

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

You can swap the endianness twice to make numpy reveal the true endianness:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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