69,727
社区成员
发帖
与我相关
我的任务
分享
struct A
{
short data;
short b:6;
short c:3;
short d:7;
}
struct A
{
short data;
#if BIG_ENDIAN
short b:6;
short c:3;
short d:7;
#else
short d:7;
short c:3;
short b:6;
#endif
}
如果不需要大小端转化
void rx_func(void*data)
{
A* a;
a = (A*)data;
a->b = 0;
a->c = 0;
a->d = 0;
}
如果需要大小端转化,我需要把结构体里的位域所在的16位做字节序转化,不知道怎么写了。
因为我要转化那16bit,没法写,因为是位域你又不能写 a->b = ntohs((UINT16*)data[0])
struct A
{
short data;
#if BIG_ENDIAN
short b:6;
short c:3;
short d:7;
#else
short d:7;
short c:3;
short b:6;
#endif
}
这根本就是错误代码!以下仅供参考:
#include <stdio.h>
#include <stdlib.h>
char buf[17];
union U {
unsigned short int aa;
struct S {
unsigned int bb:7;//(bit 0-6)
unsigned int cc:6;//(bit 7-12)
unsigned int dd:3;//(bit 13-15)
} s;
} u;
void main() {
//bbbbbbbbbbbbbbbb
//iiiiiiiiiiiiiiii
//tttttttttttttttt
//111111
//5432109876543210
//::::::::::::::::
u.aa=0xE07F;//1110000001111111
printf("bb==%d,cc==%d,dd==%d\n",u.s.bb,u.s.cc,u.s.dd);
u.s.bb=0x41;
u.s.cc=0x21;//dddccccccbbbbbbb
u.s.dd=5; //1011000011000001
printf("aa==0x%04X==%016s(2)\n",u.aa,itoa(u.aa,buf,2));
}
//bb==127,cc==0,dd==7
//aa==0xB0C1==1011000011000001(2)