Håkon Hægland
Håkon Hægland

Reputation: 40768

Numpy read big-endian data: data type format

I am trying to read data from a file with big-endian coding using NumPy fromfile function. According to the doc i figured that

I made a test file to check this:

$ echo -ne '\xfe\xdc\xba\x98\x76\x54\x32\x10' > file

However, I now get the opposite result of what I expected.

For example:

from numpy import *
import sys

print sys.byteorder

with open('file', 'rb') as fh:
    a=fromfile(fh, dtype='>u2', count=2, sep='')

print a
for i in a:
    print hex(i)

gives output:

little
[65244 47768]
0xfedc
0xba98

showing that I am on a little-endian system (the first line of output). However, I try to read data as big-endian. Should't I then get

0xdcfe
0x98ba

?

Upvotes: 3

Views: 13889

Answers (1)

RomanHotsiy
RomanHotsiy

Reputation: 5148

Actually you should not:

Let's see hexdump of file

$ hexdump -C file
00000000  fe dc ba 98 76 54 32 10

Then look at the picture from wikipedia and you'll realize that your output is correct.

enter image description here

Upvotes: 3

Related Questions