From: http://blog.csdn.net/yilin54/article/details/5683654
BIG-ENDIAN (large byte order, high byte order)
LITTLE-ENDIAN (small byte order, low byte order)
Host byte order
Network byte sequence
Java byte order
1. Big-Endian, LITTLE-ENDIAN is related to multi-byte data such as int, short, long, but has no effect on single-byte data bytes. BIG-ENDIAN is low byte emissions in the low end of memory, high byte emissions in the memory of high-end. And LITTLE-ENDIAN is exactly the opposite.
For example, int A = 0x05060708
In the case of a BIG-ENDIAN, the storage is:
Byte number 0 1 2 3
Data 05 06 07 08
In the case of a LITTLE-ENDIAN, the storage is:
Byte number 0 1 2 3
Data 08 07 06 05
2. Big-Endian, little-Endian, CPU-related, each type of CPU is not a BIG-ENDIAN is little-Endian ,. In the IA architecture, the CPU is little-Endian, while the PowerPC, iSCSI, and Motorola processors. This is actually the so-called host byte sequence. The Network byte sequence is used to determine whether the data is large or small during network upload and transmission, and the network byte sequence on the internet is big-Endian. The so-called JAVA byte sequence refers to the order in which multi-byte data is stored in the Java VM, And the java byte sequence is also big-Endian.
3. therefore, when writing a communication program using C/C ++, before sending data, you must use htonl and htons to convert integer and short integer data from the host's byte order to the network's byte order, after receiving data, ntohl and ntohs must be called to convert the network byte sequence to the host byte sequence. If the communication party is a Java program and the communication party is a C/C ++ program, you must use the above methods to convert the byte order on the C/C ++ side, on the Java side, you do not need to do any processing, because the java byte order and the network byte order are both BIG-ENDIAN, as long as the C/C ++ side can be correctly converted (from the host order to the network order before sending, reverse conversion during receiving ). If both parties are in Java, you do not need to consider the issue of byte order.
4. If all the hosts on the network are PowerPC, iSCSI and Motorola CPU, there will be no problems. However, due to the existence of a large number of IA-based CPUs, data transmission errors often occur.
5. the question raised at the beginning of the article is that the program runs on the PC server in the X86 architecture, one end of the data sending is implemented in C, and the receiving end is implemented in Java, however, before sending data, the sender does not convert the host's byte order to the network's byte order, so that the receiver receives the data of the LITTLE-ENDIAN, and the data interpretation naturally fails.
The specific data is as follows. The actual data sent is 23578
Sending data: 1A 5C
After receiving the data, what is the specific data according to the BIG-ENDIAN? You can calculate and compare it yourself!
========================================================== ========================================================== ====================
Big endian and little endian
Speaking of the byte sequence, there must be two major CPU factions. That is, Motorola's PowerPC series CPU and Intel's X86 series CPU. The PowerPC series uses the big endian method to store data, while the x86 series uses the little endian method to store data. So what is big endian and little endian?
In fact, big endian refers to the low address to store the highest valid byte (MSB), while little endian is the low address to store the lowest valid byte (LSB), that is, often said low first, high is behind.
Text descriptions may be abstract. The following uses images to describe them. For example, the storage Order of the number 0x12345678 in two different bytes of CPU is as follows:
Big endian
Low address and high address
----------------------------------------->
+-+
| 12 | 34 | 56 | 78 |
+-+
Little endian
Low address and high address
----------------------------------------->
+-+
| 78 | 56 | 34 | 12 |
+-+
From the above two figures, we can see that using the big endian method to store data is in line with our human thinking habits. And little endian ,! @ # $ % ^ & *, Go to hell-_-|
Why should we pay attention to the issue of byte order? You may ask this question. Of course, if the program you write runs only in a single-host environment and does not deal with other programs, you can ignore the existence of the byte sequence. But what if your program needs to interact with other programs? Especially when you apply your computation results on a microcomputer to a computer group. Here I want to talk about two languages. In C/C ++ programming, the data storage sequence is related to the CPU of the compilation platform, while in Java programming, the only way to store data is big endian. Imagine what will happen if you use a program written on the x86 Platform in C/C ++ to communicate with other Java programs? Take the above.
For 0x12345678, the pointer pointing to 0x12345678 is passed to the Java program. Because Java uses the big endian method to store data, naturally, it translates your data into 0x78563412. What? Is it actually another number? Yes, that's the consequence. Therefore, it is necessary to convert the byte order before your C program passes on to the Java program.
Coincidentally, all network protocols use big endian to transmit data. So sometimes weThe big endian method is called the network byte order.. When two hosts communicate in different bytes, data must be converted to network bytes before transmission. Ansi c provides four macros for converting the byte order.
========================================================== ========================================================== ======================================
/**
* Communication format conversion
*
* When Java communicates with network programs written by some windows programming languages such as C, C ++, and Delphi, corresponding conversions are required.
* Conversion between high and low bytes
* Windows starts with a low byte order.
* In Linux, Unix starts with a high byte in byte order.
* Java begins with a high byte regardless of platform changes.
*/
Public class formattransfer {
/**
* Convert the int type to the byte array after the low byte type and the high byte type.
* @ Param n int
* @ Return byte []
*/
Public static byte [] tolh (int n ){
Byte [] B = new byte [4];
B [0] = (byte) (N & 0xff );
B [1] = (byte) (N> 8 & 0xff );
B [2] = (byte) (N> 16 & 0xff );
B [3] = (byte) (N> 24 & 0xff );
Return B;
}
/**
* Convert the int type to the byte array after the front and back of the high byte.
* @ Param n int
* @ Return byte []
*/
Public static byte [] tohh (int n ){
Byte [] B = new byte [4];
B [3] = (byte) (N & 0xff );
B [2] = (byte) (N> 8 & 0xff );
B [1] = (byte) (N> 16 & 0xff );
B [0] = (byte) (N> 24 & 0xff );
Return B;
}
/**
* Convert short to the byte array with the low byte before and after the high byte.
* @ Param n short
* @ Return byte []
*/
Public static byte [] tolh (short N ){
Byte [] B = new byte [2];
B [0] = (byte) (N & 0xff );
B [1] = (byte) (N> 8 & 0xff );
Return B;
}
/**
* Convert short to the byte array with the first and lower bytes in the back.
* @ Param n short
* @ Return byte []
*/
Public static byte [] tohh (short N ){
Byte [] B = new byte [2];
B [1] = (byte) (N & 0xff );
B [0] = (byte) (N> 8 & 0xff );
Return B;
}
/**
* Convert the int type to the byte array with the front and back of the high byte.
Public static byte [] tohh (INT number ){
Int temp = number;
Byte [] B = new byte [4];
For (INT I = B. Length-1; I>-1; I --){
B = new INTEGER (temp & 0xff). bytevalue ();
Temp = temp> 8;
}
Return B;
}
Public static byte [] inttobytearray (int I ){
Byte [] abyte0 = new byte [4];
Abyte0 [3] = (byte) (0xff & I );
Abyte0 [2] = (byte) (0xff00 & I)> 8 );
Abyte0 [1] = (byte) (0xff0000 & I)> 16 );
Abyte0 [0] = (byte) (0xff000000 & I)> 24 );
Return abyte0;
}
*/
/**
* Convert float to the byte array with the front and back of the low byte
*/
Public static byte [] tolh (float f ){
Return tolh (float. floattorawintbits (f ));
}
/**
* Convert float to the byte array with the front and back of the high byte
*/
Public static byte [] tohh (float f ){
Return tohh (float. floattorawintbits (f ));
}
/**
* Convert string to byte array
*/
Public static byte [] stringtobytes (string S, int length ){
While (S. getbytes (). Length <length ){
S + = "";
}
Return S. getbytes ();
}
/**
* Convert a byte array to a string
* @ Param B byte []
* @ Return string
*/
Public static string bytestostring (byte [] B ){
Stringbuffer result = new stringbuffer ("");
Int length = B. length;
For (INT I = 0; I <length; I ++ ){
Result. append (char) (B & 0xff ));
}
Return result. tostring ();
}
/**
* Convert a string to a byte array
* @ Param s string
* @ Return byte []
*/
Public static byte [] stringtobytes (string s ){
Return S. getbytes ();
}
/**
* Convert a high byte array to an int
* @ Param B byte []
* @ Return int
*/
Public static int hbytestoint (byte [] B ){
Int S = 0;
For (INT I = 0; I <3; I ++ ){
If (B> = 0 ){
S = S + B;
} Else {
S = S + 256 + B;
}
S = S * 256;
}
If (B [3]> = 0 ){
S = S + B [3];
} Else {
S = S + 256 + B [3];
}
Return S;
}
/**
* Convert a low-byte array to an int
* @ Param B byte []
* @ Return int
*/
Public static int lbytestoint (byte [] B ){
Int S = 0;
For (INT I = 0; I <3; I ++ ){
If (B [3-I]> = 0 ){
S = S + B [3-I];
} Else {
S = S + 256 + B [3-I];
}
S = S * 256;
}
If (B [0]> = 0 ){
S = S + B [0];
} Else {
S = S + 256 + B [0];
}
Return S;
}
/**
* Conversion from a high byte array to a short
* @ Param B byte []
* @ Return short
*/
Public static short hbytestoshort (byte [] B ){
Int S = 0;
If (B [0]> = 0 ){
S = S + B [0];
} Else {
S = S + 256 + B [0];
}
S = S * 256;
If (B [1]> = 0 ){
S = S + B [1];
} Else {
S = S + 256 + B [1];
}
Short result = (short) S;
Return result;
}
/**
* Convert low-byte arrays to short
* @ Param B byte []
* @ Return short
*/
Public static short lbytestoshort (byte [] B ){
Int S = 0;
If (B [1]> = 0 ){
S = S + B [1];
} Else {
S = S + 256 + B [1];
}
S = S * 256;
If (B [0]> = 0 ){
S = S + B [0];
} Else {
S = S + 256 + B [0];
}
Short result = (short) S;
Return result;
}
/**
* Convert a high byte array to float
* @ Param B byte []
* @ Return float
*/
Public static float hbytestofloat (byte [] B ){
Int I = 0;
Float F = new flat (0.0 );
I = (B [0] & 0xff) <8 | (B [1] & 0xff) <8) | (B [2] & 0xff )) <8 | (B [3] & 0xff );
Return F. intbitstofloat (I );
}
/**
* Low-byte array conversion to float
* @ Param B byte []
* @ Return float
*/
Public static float lbytestofloat (byte [] B ){
Int I = 0;
Float F = new flat (0.0 );
I = (B [3] & 0xff) <8 | (B [2] & 0xff) <8) | (B [1] & 0xff )) <8 | (B [0] & 0xff );
Return F. intbitstofloat (I );
}
/**
* Arrange the elements in the byte array in reverse order.
*/
Public static byte [] bytesreverseorder (byte [] B ){
Int length = B. length;
Byte [] result = new byte [length];
For (INT I = 0; I <length; I ++ ){
Result [length-i-1] = B;
}
Return result;
}
/**
* Print byte array
*/
Public static void printbytes (byte [] bb ){
Int length = BB. length;
For (INT I = 0; I <length; I ++ ){
System. Out. Print (BB + "");
}
System. Out. println ("");
}
Public static void logbytes (byte [] bb ){
Int length = BB. length;
String ut = "";
For (INT I = 0; I <length; I ++ ){
Ut = out + BB + "";
}
}
/**
* Convert the int type value to the corresponding int value in byte order.
* @ Param I int
* @ Return int
*/
Public static int reverseint (int I ){
Int result = formattransfer. hbytestoint (formattransfer. tolh (I ));
Return result;
}
/**
* Convert the value of the short type to the corresponding short value in byte order.
* @ Param s short
* @ Return short
*/
Public static short reverseshort (short S ){
Short result = formattransfer. hbytestoshort (formattransfer. tolh (s ));
Return result;
}
/**
* Convert a float value to an inverted byte order and return the corresponding float value.
* @ Param F float
* @ Return float
*/
Public static float reversefloat (float f ){
Float result = formattransfer. hbytestofloat (formattransfer. tolh (f ));
Return result;
}
}