1. When it comes to byte-order, a friend asks what is a byte-order
Very easy: "Like a 16-bit integer." Consists of 2 bytes, 8 bits is one byte, some systems place high bytes on low memory addresses, others place low bytes on high memory addresses, so there is a problem with the byte order. 】
2. So what is high-byte, low-byte?
It is also quite simple: "A 16 binary integer has two bytes, for example: 0xa9."
The high byte refers to the first 8 bits of the 16 binary number (8 bits with high weights), as in the previous example.
The low byte refers to the last 8 bits of the 16 binary number (8 bits with low weight), as in the previous example 9.
】
Variable types that are larger than one byte generally have two methods of representation:
For example: Variable 0XABCD in the big-endian byte-order and small-end byte-type system representation method
We use code to verify whether our own system is small or big-endian.
#include <stdio.h>/ * Variable type of union type. The high and low end bytes for the test byte order * Member value can be interviewed by the member type in bytes */typedef union{UNSIGNED Short int value;/ * Short Integer variable * /UnsignedChar byte[2];/ * Character type * /}to;intMainintargcChar*ARGV) {to Typeorder;/ * A To type variable * /Typeorder.value=0XABCD;/ * Assign the Typeorder variable a value of 0XABCD * / / * Small endian check * / if(Typeorder.byte[0] ==0xCD&& Typeorder.byte[1]==0xAB){/ * Low byte in front * /printf"Low endian byte order" "byte[0]:0x%x,byte[1]:0x%x\n", Typeorder.byte[0], Typeorder.byte[1]); }/ * Big endian byte order check * / if(Typeorder.byte[0] ==0xAB&& Typeorder.byte[1]==0xCD){/ * High byte in front * /printf"High endian byte order" "byte[0]:0x%x,byte[1]:0x%x\n", Typeorder.byte[0], Typeorder.byte[1]); }return 0; }
3. Introduction to Byte-order conversion functions
Use of byte-order conversion functions:
#include <stdio.h>/ * Variable type of union type, used for test byte order * The high and low byte of member value can be interviewed by the member type in bytes *// * 16-bit * /typedef union{UNSIGNED Short int value; UnsignedChar byte[2]; }to16;/ * 32-bit * /typedef union{UNSIGNEDLong int value; UnsignedChar byte[4]; }to32;#Define BITS16 16/*16 bit * /#Define BITS32 32/*32 bit * //* According to Byte printing, begin is byte start, * flag for BITS16 16 bits, * flag for BITS32 32 bits. */voidShowvalue (unsignedChar*begin,intFlag) {intnum =0, i =0;if(flag = = BITS16) {num =2; }Else if(flag = = BITS32) {num =4; } for(i =0; i< num; i++) {printf ("%x", * (Begin+i)); } printf ("\ n");}intMainintargcChar*ARGV) {to16 v16_orig, v16_turn1,v16_turn2;/ * A to16 type variable * /To32 V32_orig, v32_turn1,v32_turn2;/ * A TO32 type variable * /V16_orig.value=0XABCD;/ * Assignment is 0XABCD * /V16_turn1.value= Htons (V16_orig.value);/ * First time conversion * /V16_turn2.value= Ntohs (v16_turn1.value);/ * Second conversion * /V32_orig.value=0x12345678;/ * Assign value to 0x12345678 * /V32_turn1.value= Htonl (V32_orig.value);/ * First time conversion * /V32_turn2.value= Ntohl (v32_turn1.value);/ * Second conversion * / / * Print results * /printf"host to network byte order change:\n"); printf"\torig:\t"); Showvalue (V16_orig.byte, BITS16);/ * Raw values for 16-bit values * /printf"\t1 Times:"); Showvalue (v16_turn1.byte, BITS16);/ * The first converted value of the 16-bit value * /printf"\t2 Times:"); Showvalue (v16_turn2.byte, BITS16);/ * The value after the second conversion of the 16-bit value * /printf"host to network byte order change:\n"); printf"\torig:\t"); Showvalue (V32_orig.byte, BITS32);/ * Raw values for 32-bit values * /printf"\t1 Times:"); Showvalue (v32_turn1.byte, BITS32);/ * The first converted value of the 32-bit value * /printf"\t2 Times:"); Showvalue (v32_turn2.byte, BITS32);/ * The value after the second conversion of the 32-bit value * / return 0; }
Linux Network Programming--byte order