7

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 4
    I might be missing your question here, but just check the native byteorder with `sys.byteorder`? – miradulo Apr 09 '18 at 19:45
  • 1
    Maybe this? https://stackoverflow.com/a/1346039/4909087 – cs95 Apr 09 '18 at 19:46
  • @miradulo. I think you are understanding perfectly. I am very surprised that there is no way to get that from numpy directly, given that numpy has to access that information at some point, presumably. – Mad Physicist Apr 09 '18 at 19:49

2 Answers2

14

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',
}
user2357112
  • 260,549
  • 28
  • 431
  • 505
2

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

dtype_nonnative = dtype.newbyteorder('S').newbyteorder('S')
dtype_nonnative.byteorder
Eric
  • 95,302
  • 53
  • 242
  • 374