user-0 提问: 还是十六进制数据解析的问题，上个回帖有的数据是不正确的，特请教

回复:
user: user-0
content: 谢谢了  赵四老师

user: user-1
content: 引用 2 楼 2004v2004的回复:谢谢 赵四老师   能解释下   for (int i=0;i<NBC;i++) {         swapbyte(0+(char *)&(data.Channel[i].Data),3+(char *)&(data.Channel[i].Data));         swapbyte(1+(char *)&(data.Channel[i].Data),2+(char *)&(data.Channel[i].Data));     } 这段代码吗  没看懂 将0123四个字节03交换，12交换，达到大端字节顺序转换为小端字节顺序的目的。

user: user-0
content: 谢谢 赵四老师   能解释下   for (int i=0;i<NBC;i++) {         swapbyte(0+(char *)&(data.Channel[i].Data),3+(char *)&(data.Channel[i].Data));         swapbyte(1+(char *)&(data.Channel[i].Data),2+(char *)&(data.Channel[i].Data));     } 这段代码吗  没看懂

user: user-1
content: #pragma warning(disable:4996 4838) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS” #include <stdio.h> #include <string.h> #include <malloc.h> #include <stdlib.h> #pragma pack(push) #pragma pack(1) struct CHANNEL {     short int Stat;     float Data; }; struct DATA {     char Start;     char Address[4];     char Command[2];     char BinaryBlocSize[2];     char StartChannel;     unsigned char NumberOfChannel;     struct CHANNEL *Channel; } data; #pragma pack(pop) void swapbyte(char *a,char *b) {     char t;     t=*a;*a=*b;*b=t; } int main() {     int NBC;     char b0[]={         0x1B,         0x41,0x46,0x32,0x32,         0x32,0x34,         0x00,0x2C,         0x01,         0x07,          0x02,0x00,         0xBD,0xD6,0xD4,0x57,          0x02,0x00,         0x43,0xF5,0x14,0x03,          0x02,0x00,         0x43,0x18,0x48,0x9B,          0x02,0x00,         0x44,0x77,0xAF,0xE5,          0x02,0x00,         0x41,0xA0,0x21,0x12,          0x02,0x00,         0x42,0x48,0x02,0xAB,          0x02,0x00,         0x45,0x24,0x5B,0x7C,          0x41,0x36,         0x03};     memcpy((void *)&data,(const void *)&b0[0],1+4+2+2+1+1);     NBC=(int)(data.NumberOfChannel);     printf("NBC:%d\n",NBC);     printf("sizeof(struct CHANNEL):%d\n",sizeof(struct CHANNEL));     data.Channel=(struct CHANNEL *)malloc(NBC*sizeof(struct CHANNEL));     memcpy((void *)(data.Channel),(const void *)&b0[1+4+2+2+1+1],NBC*sizeof(struct CHANNEL));     for (int i=0;i<NBC;i++) {         swapbyte(0+(char *)&(data.Channel[i].Data),3+(char *)&(data.Channel[i].Data));         swapbyte(1+(char *)&(data.Channel[i].Data),2+(char *)&(data.Channel[i].Data));     }     for (int i=0;i<NBC;i++) {         printf("%d 0x%04hX %f\n",i,data.Channel[i].Stat,data.Channel[i].Data);     }     free(data.Channel);       char b1[]={0x1B,0x4F,0x33,0x34,0x32,0x32,0x34,0x00,0x08,0x01,0x01,0x02,0x00,0x41,0x65,0x5A,0xB9,0x42,0x31,0x03};      memcpy((void *)&data,(const void *)&b1[0],1+4+2+2+1+1);     NBC=(int)(data.NumberOfChannel);     printf("NBC:%d\n",NBC);     printf("sizeof(struct CHANNEL):%d\n",sizeof(struct CHANNEL));     data.Channel=(struct CHANNEL *)malloc(NBC*sizeof(struct CHANNEL));     memcpy((void *)(data.Channel),(const void *)&b1[1+4+2+2+1+1],NBC*sizeof(struct CHANNEL));     for (int i=0;i<NBC;i++) {         swapbyte(0+(char *)&(data.Channel[i].Data),3+(char *)&(data.Channel[i].Data));         swapbyte(1+(char *)&(data.Channel[i].Data),2+(char *)&(data.Channel[i].Data));     }     for (int i=0;i<NBC;i++) {         printf("%d 0x%04hX %f\n",i,data.Channel[i].Stat,data.Channel[i].Data);     }     free(data.Channel);       char b2[]={0x1B,0x41,0x43,0x33,0x32,0x32,0x34,0x00,0x14,0x01,0x03,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x39,0x33,0x03};      memcpy((void *)&data,(const void *)&b2[0],1+4+2+2+1+1);     NBC=(int)(data.NumberOfChannel);     printf("NBC:%d\n",NBC);     printf("sizeof(struct CHANNEL):%d\n",sizeof(struct CHANNEL));     data.Channel=(struct CHANNEL *)malloc(NBC*sizeof(struct CHANNEL));     memcpy((void *)(data.Channel),(const void *)&b2[1+4+2+2+1+1],NBC*sizeof(struct CHANNEL));     for (int i=0;i<NBC;i++) {         swapbyte(0+(char *)&(data.Channel[i].Data),3+(char *)&(data.Channel[i].Data));         swapbyte(1+(char *)&(data.Channel[i].Data),2+(char *)&(data.Channel[i].Data));     }     for (int i=0;i<NBC;i++) {         printf("%d 0x%04hX %f\n",i,data.Channel[i].Stat,data.Channel[i].Data);     }     free(data.Channel);       return 0; } //NBC:7 //sizeof(struct CHANNEL):6 //0 0x0002 -0.104897 //1 0x0002 490.156342 //2 0x0002 152.283615 //3 0x0002 990.748352 //4 0x0002 20.016148 //5 0x0002 50.002605 //6 0x0002 2629.717773 //NBC:1 //sizeof(struct CHANNEL):6 //0 0x0002 14.334649 //NBC:3 //sizeof(struct CHANNEL):6 //0 0x8000 0.000000 //1 0x8000 0.000000 //2 0x8000 0.000000 //