利用的就是计算机存储的特性,需要注意大小端
1.
char sDec[4] = {0x05,0xc5,0xb3,0xea};
低字节序的情况下. 并且 int 占用 32bit.
char sDec[4] = {0x05,0xc5,0xb3,0xea};
int val = 0;
char *pBuf = (char*)&val;
pBuf[0] = sDec[3];
pBuf[1] = sDec[2];
pBuf[2] = sDec[1];
pBuf[3] = sDec[0];
这时val==0x05c5b3ea;
2.
如果是高字节序那就更简单了.
char sDec[4] = {0x05,0xc5,0xb3,0xea};
int val = *(int*)sDesc;
typedef union {
int addr;
char part[4];
} ip;
ip ip1;
ip1.part[0] = 0x01;
ip1.part[1] = 0x02;
ip1.part[2] = 0x03;
ip1.part[3] = 0x04;
printf("%d\n", ip1.addr);
注意大小端