Network byte order, large end method, small end method

xiaoxiao2021-04-10  473

In the country's 4 SMS protocols, the headers of the four byte lengths are defined, and the data type of the field is unsigned (that is, the range of this field is 0-2 ^ 16-1) In the Java language, there is no symbolic shaping such data type (if INT type is used, since the int model in Java is a symbol number, it will send overflow), I envishable MSSAGE LENGTH in the long type The size of the number is controlled within 0-2 ^ 16-1, and the range is restarted by zero.

When transmitting online, the long type is first converted to a BYTE array, steps below:

Long L; Byte [] B; B [0] = (Byte) (L >>> 24); B [1]] = (Byte) (L >>> 16); B [2]] = (Byte) (L >>> 8); B [3]] = (byte) (L); at this time, in B [], in the network byte order (large terminal method, the high data of L is stored in Byte [] The low address, because the address is stored from low-tote high-developed data using OutputStream Public Void Write (Byte [] B, INT OFF, INT LEN) method to write byte, write Byte [ 0] Bytes to byte [3]. Java.io Class OutputStream Write Public Abstract Void Write (int b) THROWS IOEXCEPTION

Writes the specified byte to this output stream. The general contract for

Write is that one byte is written to the output stream. The byte to be written is the year low-order bits of the argument

b. The 24 high-order bits of

B is ignored.

Subclasses of OutputStream Must Provide An Implementation for this Method.

Parameters:

B - the

Byte.

Throws:

IOEXCEPTION - IEXCEPTION - IF AN I / O Error Occurs. In particular, An

IOEXCEPTION May Be Thrown if The Output stream has been closed.

Write public void write (byte [] b, int off, int LEN) throws oException

Writes

Len Bytes from the specified byte array starting at offset

Off to this Output Stream. The General Contract for

Write (b, off, len) is that thing some of the bytes in the array

B Are Written to The Output Stream in Order; Element

B [OFF] is the first byte Written and

b [off len-1] is the last byte written by this operation.The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more Efficient Implementation.

IF B Is Null, a Nullpointerexception IS Thrown.

IF Off Is Negative, or Off Len Is Greater Than The Length of The Array B, The An IndexOfboundsexception is Thrown.

Parameters:

B - The Data.

OFF - The Start Offset in The Data.

Len - the number of bytes to write.

Throws:

IOEXCEPTION - IEXCEPTION - IF AN I / O Error Occurs. In particular, An

IOEXCEPTION IS thrown if the output stream is closed.

------ About the network, host byte sequence articles http://www-128.ibm.com/developerworks/cn/java/l-datanet/index.html Transformation of the host and network word sequence

Recently, use C # to develop network development, you need to process ISO8583 packets, because some of which are numerical, so that the word sequencing is involved in the transmission. The byte order is the order in which data is more than one byte type is stored in memory, usually there are two byte sequences, according to their location, we are called host sequence and network word sequence, respectively.

Usually we believe that the network word sequence is a standard order, and the host word sequencing is converted to a network word sequence, and the network word sequencing is converted to the host word sequence. I thought I had to write a function yourself. In fact, the network library has been provided.

Host to the network: Short / int / long ipaddress.hosttonetworkOrder (Short / INT / Long)

Network to the host: short / int / long ipaddress.networktohostorder (short / int / long): SHORT / INTWORKTOHOSTORDER (Short / INT / Long)

Host word sequence Individual Low byte data is stored at the memory low address, high byte data is stored at the memory high address, such as:

INT x = 1; // At this time, X is the host word sequence: [1] [0] [0] [0] low to high

INT Y = 65536 // This time y is the host word sequence: [0] [0] [1] [0] low to high

We convert X and Y through the transition function of the host to the network word sequence to get the network byte sequence value of their corresponding network, and the network sequence is high byte data stored at the low address, the low-byte data is stored in the high address. Position, such as:

INT M = ipaddress.hosttonetworkOrder (x);

/ / At this time, M is the host word sequence: [0] [0] [0] [1] high to low

INT n = ipaddress.hosttonetworkOrder (Y);

// This time n is the host word sequence: [0] [1] [0] [0] high to low

After the conversion, we can pass

Byte [] btvalue = bitconverter.getbytes (m); get a BYTE array of length 4, and then set this array to the corresponding position of the message to send out.

Similarly, after receiving the message, you can split the message according to the domain, get Btvalue, use

INT m = bitconverter.Toint32 (btvalue, 0); // Spend from the 0th bit of Btvalue

Get the value of this domain, it is not possible to use it directly, and the network to the host word sequence should be converted:

INT x = ipaddress.networktohostorder (m);

The X obtained at this time is the actual value in the packet. -------------------------------------------------- - That is also talking about the character. Http://bigwhite.blogbus.com/logs/2005/09/. The platform transplantation of Sun SPARC to Intel X86 makes our procedure "byte order problem", since it encountered Learn in depth. First, the word sequence definition word sequence, the order of the asidi, and then saying that the two sentences are greater than one byte type of data in memory (one byte data, there is of course no need to talk about the order) . In fact, most people are rarely dealing with bytes in the actual development. Only in cross-platform and in the network program are a problem that should be considered. The byte sequence is two categories: BIG-Endian and Little-Endian are mentioned in all articles of the Introduction. The definition of the standard BIG-Endian and Little-Endian is as follows: a) Little-endian is the low-end address of the memory in the memory, and the high byte is discharged at the memory high address. b) BIG-Endian is the low address of the memory in the high byte, and the low byte is discharged in memory. c) Network word sequence: TCP / IP layer protocol defines the byte sequence as a BIG-ENDIAN, so the byte order used in the TCP / IP protocol is usually called network word sequence. In fact, I was very confused when I first saw this definition. I was also awkward after I saw a few examples. What is the high / low address? What is the high low? After reading some information, there is a slight experience. Second, high / low address and high and low byte first we must know the spatial layout of memory in our C program image: In "C Expert Program" or "Unix Environment Advanced Programming", there is a description of the memory space layout, roughly As shown below: ---------------------- The highest memory address 0xffffffff | Stack.. Stack. Stack Top ----------- ------------ | | / | / NULL (Empty) / | / | | ---------------------- Pile ---------------------- Unexpected data ---------------- (collective data segment) initialization Data -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Minimum memory address 0x00000000 or above as an example If we assign a unsigned char buf [4] on the stack, how do this array variables are layout on the stack [Note 1]? Look at the picture: Stack (high address) ---------- BUF [3] BUF [2] buf [1] buf [0] ---------- Stack top (low Address) Now let's clarify the high and low address, then I will find high / low bytes. If we have a 32-bit unsigned integer 0x12345678 (huh, it happens to see the four bytes of BUF above as a whole Type), then what is the high position, what is the low level? it's actually really easy. In the decimal system, we have said that the left is high, and the lower position is the same, in other environments. Take 0x12345678, from the high to low byte, 0x12, 0x34, 0x56 and 0x78. The high and low address and the high and low byte are clarified.

Let's review the definition of BIG-Endian and Little-Endian, and use the illustration to illustrate two word sequences: Take unsigned int value = 0x12345678 as an example, see the storage situation in two bytes, we can use Unsigned char buf [4] to represent value: big-endian: low address storage high, as shown below: Stack (high address) --------------- BUF [3] (0x78) - Low BUF [2] (0x56) BUF [1] (0x34) BUF [0] (0x12) - High --------------- Stack Top (low address) Little- Endian: Low address storage, as shown below: Stack (high address) --------------- BUF [3] (0x12) - High BUF [2] (0x34) BUF [ 1] (0x56) BUF [0] (0x78) - Low ------------- Stack Top (low address) In the existing platform, Intel's X86 is Little- Endian, as SUN's SPARC is BIG-Endian. Third, Example Test Platform: Sun SPARC Solaris 9 and Intel X86 Solaris 9 Our example is this: Use the same program to read the same binary file on the platform using different word sequences.

The process of generating binary files is as follows: / * gen_binary.c * / int main () {file * fp = null; int value = 0x12345678; int RV = 0; fp = fopen ("temp.dat", "wb"); IF (fp == null) {Printf ("fopen error / n"); return -1;} rv = fwrite (& value), 1, fp); if (rv! = 1) {Printf (" FWRITE ERROR / N "); return -1;} fclose (fp); Return 0;} The program reads the binary file is as follows: int main () {int value = 0; file * fp = null; int RV = 0; Unsigned char buf [4]; fp = fopen ("Temp.dat", "RB"); if (fp == null) {Printf ("fopen error / n"); return -1;} RV = FREAD (BUF , SizeOf (unsigned char), 4, fp; if (rv! = 4) {Printf ("FREAD ERROR / N"); return -1;} Memcpy (& Value, BUF, 4); // OR value = * ((int *) BUF); Printf ("The Value I S% x / n ", value); fclose (fp); return 0;} Test process: (1) Generate a Temp.dat file under the SPARC platform to read the result of the TEMP.DAT file under the SPARC platform: The value is 12345678 Read the Temp.dat file under the X86 platform: The value is 78563412 (1) Generate the temp.dat file under the X86 platform to read the result of the TEMP.DAT file under the SPARC platform: The value is 78563412 at the X86 platform Read the result of the temp.dat file: The value is 12345678 [Note 1] BUF [4] In the stack layout, I also obtained the example program: int main () {unsigned char buf [4]; printf ("THE BUF [0] Addr IS% X / N ", BUF); Printf (" "The BUF [1] AddR IS% X / N", & Buf [1]); return 0;} Output: SPARC platform:

转载请注明原文地址:https://www.9cbs.com/read-133393.html

New Post(0)