Byte order conversion and reading of the structure position field value, bitfield

Source: Internet
Author: User

Byte order conversion and reading of the structure position field value, bitfield

Recently I encountered another problem several years ago. Mark it.

 

For a cross-byte field, if the byte order of the environment before and after data transmission is different (LE-> BE, BE-> LE ), A simple call (ntohs/ntohl/htons/htonl) does not read the value of a bit field correctly.

For example:

struct _exam_{  unsigned int tag : 6;  unsigned int field1 : 3;  unsigned int field2 : 7;  unsigned int field3 : 11;  unsigned int pad :  5;}Exam;

 

Tag, field2, pad are the inner byte fields, and field1 and field3 are cross-byte fields. When this struct is transmitted from a large-end ordering platform (such as MIPS, big-endian) to a PC (little-endian) over a network, it directly uses Exam regardless of whether ntohl is called or not. field3 statements cannot obtain the correct value.

 

Solution:

(1) Call ntohl and save the variable as an unsigned int.

At this time, the five members of the Exam will BE arranged in the order of addresses from high to low (in the correct case, the members inside the struct should follow the order of BE and LE, from low to high ).

 unsigned int temp = 0; memcpy(&temp, sizeof(temp), &Exam); temp = ntohl(temp);  

 

(2) Use bitwise operators to obtain the correct values of bitwise fields.

After the previous step, although the Exam members (the bitstream in temp is treated as Exam) are arranged in an incorrect order in the memory, we will make an error. From the perspective of the "logical view" of daily printing and writing, Exam. the tag is at the "leftmost" (high), Exam. pad is at the "rightmost" (low level), and the bit order in the five bit regions is correct. Therefore, you can directly use the shift operator to obtain the value.

Struct _ exam _ ExamWin32; ExamWin32.tag = temp> 26; // all the high positions are 0. Do not use bitwise and calculation ExamWin32.field1 = (temp> 23) & 0x00000007; // only three digits lower than the result of the shift operation are obtained. ExamWin32.field2 = (temp> 16) & 0x0000007F; ExamWin32.field3 = (temp> 5) & 0x000007FF; ExamWin32.pad = temp & 0x0000001F;

 

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.