Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 760
Mad Physicist Avatar asked Apr 09 '18 19:04

Mad Physicist


2 Answers

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',
}
like image 193
user2357112 supports Monica Avatar answered Sep 30 '22 08:09

user2357112 supports Monica


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

dtype_nonnative = dtype.newbyteorder('S').newbyteorder('S')
dtype_nonnative.byteorder
like image 31
Eric Avatar answered Sep 30 '22 10:09

Eric