C # java description of "byte order.

Source: Internet
Author: User

Description of "byte order" FavoritesBIG-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 does not affect the bytes of single-byte data. 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 data is large or small during network upload and data 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, no processing is required, because JAVA and network 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 sides of the communication are JAVA, you do not need to consider the question 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. However, 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 the C/C ++ programming language, the data storage sequence is related to the CPU where the compilation platform is located, while the JAVA program uses the big endian method only to store data. 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 0x12345678 as an example. 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, conversion between high and low bytes * windows starts with low bytes * linux, and unix starts with a high byte * java regardless of platform changes, all begin with a high byte */public class FormatTransfer {/*** convert int to a low byte before, high byte array * @ 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 int to high byte before, low byte array * @ 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 a low byte before, high byte array * @ 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 a high byte before, low byte array * @ 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 int to high byte before, the byte array after the low byte is 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 before and after low bytes */public static byte [] toLH (float f) {return toLH (Float. floatToRawIntBits (f);}/*** convert float into a byte array before and after High bytes */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 the byte array to 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
 
  
<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 the low byte array to 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 ;} /*** convert high byte array to 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 the high byte array to float * @ param B byte [] * @ return float */public static float hBytesToFloat (byte [] B) {int I = 0; float F = new Float (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 converted to float * @ param B byte [] * @ return float */public static float lBytesToFloat (byte [] B) {int I = 0; Float F = new Float (0.0); I = (B [3] & 0xff) <8 | (B [2] & 0xff) <8) | (B [1] & 0xff) <8 | (B [0] & 0xff ); return F. intBitsToFloat (I);}/*** sorts 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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.