Big-end and small-end conversion, le32_to_cpu and cpu_to_le32

Source: Internet
Author: User
Big-end small-end conversion, le32_to_cpu and cpu_to_le32 byte order

Http://oss.org.cn/kernel-book/ldd3/ch11s04.html

Be careful not to assume the byte order. the multi-byte value of PC storage is low-byte first (small-end first, so small-end). Some advanced platforms work in another way (large-end. your code should be written in this way whenever possible, and it does not care about the byte sequence of the data it operates on. however, sometimes a driver needs to use a single byte to establish an integer or vice versa, or it must communicate with a device that requires a specific sequence.

Include File <ASM/byteorder. h> defined or _ big_endian or _ little_endian, which depends on the byte sequence of the processor. when dealing with the byte order problem, you may encode a bunch of # ifdef _ litttle_endian condition statements, but there is a better method. the Linux kernel defines a set of macro definitions to process conversions between the processor in bytes and the data you need to store and load in a specific byte order. for example:

u32 cpu_to_le32 (u32);u32 le32_to_cpu (u32);

The two macros are defined to convert a value, from whatever CPU usage to an unsigned, small-end, 32-digit, and back. whether your CPU is small or large, whether it is a 32-bit processor or not. they return their parameters if there is no need to do so. using these macro definitions makes it easy to write portable code without having to compile and build it using a large number of conditions.

There are many similar functions. You can see the complete list in <Linux/byteorder/big_endian.h> and <Linux/byteorder/little_endian.h>. after a while, this mode is not difficult to follow. be64_to_cpu converts an unsigned, large-end, 64-bit value to an internal CPU. le16_to_cpus. On the contrary, it processes signed, small, and 16-digit data. when processing the pointer, you will also use cpu_to_le32p, which uses a pointer to a value for conversion, rather than the value itself.
For the rest, see include files.

 

Then when do we use le32_to_cpu (), and when do we use cpu_to_le32 ()?
>
> The names tell it.

> Le32_to_cpu is used for convesions from 32bit little endian data into CPUs endianness

> Cpu_to_le32 is used for convesions from CPU endianness to little endian 32bit data.
> Actually, both macros do the same thing, but one shoshould make the differences clear to make code more readable so that anyone can quickly find out whether some data is kept in native endianness or some special one.

You can use the following method to determine the mode used by the processor.

Int getendianness () {short S = 0x0110; char * P = (char *) & S; If (P [0] = 0x10) return 0; // Small End format else return 1; // Large End format}

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.