Size End-order conversions

Source: Internet
Author: User

in embedded development, often encountered errors caused by improper use of the end sequence, it is decided decided to comb the common functions of the work, one is to avoid making the same mistakes, the second is to facilitate the query later. This paper is divided into four parts: 1, what is the size of the end sequence, 2, the size of the end sequence data conversion function, 3, the application scenario, 4, the use of summary.

  1, what is the size of the end sequence

Big-endian mode: The high byte of data is kept in the low address of memory, and the low byte of data is kept in high address of memory.

Small-end mode: Refers to the high byte of data stored in the high address of memory, and the low byte of data is kept in low memory address.

Now, take a unsigned int integer data 0x12345678 as an example, its big-endian, small-endian storage content.

  

  2, the size of the end sequence data conversion function

A) Convert the unsigned char array into a "big endian" integer;

/** function:conversearraytobeui* Description: Converts unsigned character array data to "big endian" integer * Parameter:srcarray-    -[in] Source Array data * Desbedata--[out] target "big endian" Integer * Return 0 Success * Non 0 Failure * NOTE: * Other:*/intMulconverse_call Conversearraytobeui (unsignedChar*srcarray,unsignedint*desbedata) {    if(Srcarray = = Null_point | | desbedata = =null_point) {        returnErr_null_point; }    *desbedata = (unsignedint) (srcarray[0]<< -) + (unsignedint) (srcarray[1]<< -) +(unsignedint) (srcarray[2]<<8) + (unsignedint) srcarray[3]; return_success;}

b) Convert the unsigned char array into a "small-endian" integer;

/** function:conversearraytoleui* Description: Convert unsigned character array data to "small endian" integer * Parameter:srcarray-    -[in] Source Array data * Desledata--[out] target "small endian" integer * Return 0 Success * Non 0 Failure * NOTE: * Other:*/intMulconverse_call Conversearraytoleui (unsignedChar*srcarray,unsignedint*desledata) {    if(Srcarray = = Null_point | | desledata = =null_point) {        returnErr_null_point; }    *desledata = (unsignedint) (srcarray[3]<< -) + (unsignedint) (srcarray[2]<< -) +(unsignedint) (srcarray[1]<<8) + (unsignedint) srcarray[0]; return_success;}

c) The integer is stored in the array in the "big endian" format;

/** function:converseuitobearray* Description: An array of unsigned characters that convert unsigned integers to "big endian" Storage * parameter:srcdata                    --[in] Source integer * Desbearray--[out] target "big endian" stored array data * Return 0 Success * Non 0 failure * NOTE: * Other:*/intMulconverse_call Converseuitobearray (unsignedintsrcdata,unsignedChar*Desbearray) {    if(Desbearray = =null_point) {        returnErr_null_point; } desbearray[0] = (unsignedChar) (srcdata>> -); desbearray[1] = (unsignedChar) (srcdata>> -); desbearray[2] = (unsignedChar) (srcdata>>8); desbearray[3] = (unsignedChar) Srcdata; return_success;}

d) The integers are stored in the array in the "small-endian" format.

/** function:converseuitolearray* Description: Convert unsigned integers to an array of unsigned characters stored as "small endian" * parameter:srcdata                    --[in] Source integer * Deslearray--[out] target "small endian" stored array data * Return 0 Success * Non 0 failure * NOTE: * Other:*/intMulconverse_call Converseuitolearray (unsignedintsrcdata,unsignedChar*Deslearray) {    if(Deslearray = =null_point) {        returnErr_null_point; } deslearray[3] = (unsignedChar) (srcdata>> -); deslearray[2] = (unsignedChar) (srcdata>> -); deslearray[1] = (unsignedChar) (srcdata>>8); deslearray[0] = (unsignedChar) Srcdata; return_success;}

  3. Application Scenario

PC (small end order) sends data to Chip (big endian)

Assuming that the PC generates an integer value 0x00000001, through the communication interface (such as the serial port) to send data to the chip, according to the transmission of data one byte at a time, then the chip will receive 4 bytes of data, the content according to the address from low to high order of 0x01,0x00,0x00,0x00, Then the chip will assume that the integer value it receives is 0x01000000.

at this point, the two hardware platform will be due to the difference between the size of the data send and receive errors, there are two ways to solve the problem: 1, before the PC sends data, through the conversion function Converseuitobearray, converts an integer value 0x00000001 into an array of [00,00,00,01] characters, which is then sent to the chip via a communication interface, 2. After the chip receives 4 bytes of data, the function Conversearraytoleui, convert [01,00,00,00] to Integer 0x00000001.

  4. Summary of Use

(1) Different hardware platform if the same type of end-order, the calculation/use of no need to turn the order;

(2) Different hardware platforms with different types of end-order, but the transmitted character array "without" conversion to multi-byte basic type of data (such as int,double,float), calculation/use without the need to turn the order;

(3) Different hardware platforms if different types of end-order are used, but the transmitted character array "needs" to be converted to multi-byte basic type data (such as Int,double,float), the calculation/use needs to be translated;

Size End-order conversions

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.