Mad Physicist
Mad Physicist

Reputation: 114390

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.

Upvotes: 7

Views: 6850

Answers (2)

Eric
Eric

Reputation: 97631

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

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

Upvotes: 2

user2357112
user2357112

Reputation: 281330

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',
}

Upvotes: 15

Related Questions