Endian is the byte order in a binary file.
For example, suppose some data is stored in 2 bytes.
00000001 00000000
At this time, which byte indicates a value from 0 to 255 (that is, from the smallest digit to the 1,2,4,8 ... digit).
The other represents a value between 256 and 65535 (ie, from the smallest to 256,512,1024 ...).
For big endian, the first byte represents a larger number. In the above example, the first byte is in the order of 256 to 32768, and the second byte is in the order of 1 to 128. This is the same number we usually see.
On the contrary, in the case of little endian, the first bite is smaller.
In addition, the last number in the byte is still the smallest (?) (← I'm not sure, so please comment if you understand)
In modern times, many systems have adopted Intel chips, so both Win and Mac have become little endian, but big endian data is rolling around in the streets.
If you touch the data of big endian with the intention of little endian, the result will be strange and it will hurt, so be careful. This page summarizes how to switch endianness (for my favorite language).
Fortran In Fortran, there is a method of originally determining the endianness at compile time and a method of switching when reading a file in a program.
This is super easy.
If you are using gfortran
, compile it as follows.
However, be aware that everything in the program will be big endian.
gfortran -fconvert=big-endian -o test test.f90
export F_UFMTENDIAN="little;big 87"
This makes basic little endian
and device number 87 big endian
.
Of course, you can flip big
and small
over.
You can omit the rest from ;
.
Python Easy to decide when reading a file. For example, suppose you want to read data to create an image. First, put the data of the opened file in the array.
imagearr=np.fromfile(rfile,'<h',NROWS*NCOLS*1)
An array that contains the original data that ʻimagearrcreates the image for.
npis numpy.
rfileis the directory of files.
NROWS * NCOLS * 1` indicates the number of rows x the number of columns.
Here, <h
indicates the type of variable to be read as endian.
The first <
indicates little endian.
h
indicates int16.
Other examples are shown below.
characters th> | byte order th> | size th> | Alignment th> |
---|---|---|---|
@ | native td> | native | native |
= | native td> | standard | none |
< | Little endian td> | standard | none |
> | Big endian td> | standard | none |
! | Network (= Big Endian) td> | standard | none |
Format th> | Type in C th> | Python type th> | standard size th> | Remarks th> |
---|---|---|---|---|
x | pad byte | no value | ||
c | char | String of length 1 td> | 1 | |
b | signed char | Integer type (integer) td> | 1 | (3) |
B | unsigned char | Integer type td> | 1 | (3) |
? | _Bool | boolean type (bool) td> | 1 | (1) |
h | short | Integer type td> | 2 | (3) |
H | unsigned short | Integer type td> | 2 | (3) |
i | int | Integer type td> | 4 | (3) |
I | unsigned int | Integer type td> | 4 | (3) |
l | long | Integer type td> | 4 | (3) |
L | unsigned long | Integer type td> | 4 | (3) |
q | long long | Integer type td> | 8 | (2), (3) |
Q | unsigned long long | Integer type td> | 8 | (2), (3) |
f | float | Floating point type td> | 4 | (4) |
d | double | Floating point type td> | 8 | (4) |
s | char[] | string td> | ||
p | char[] | string td> | ||
P | void * | Integer type td> | (5), (3) |
Source: http://docs.python.jp/2/library/struct.html
Recommended Posts