位元組序問題 u8u32快速賦值問題 待整理
阿新 • • 發佈:2018-12-13
#include<stdio.h> #include <string.h> typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; void u8_to_u32(unsigned char* u8,unsigned int *u32) { if(0) *u32 = (u8[0]<<24)|(u8[1]<<16)|(u8[2]<<8)|u8[3]; else *u32 = (u8[3]<<24)|(u8[2]<<16)|(u8[1]<<8)|u8[0]; } int main(void) { unsigned int u32 = 0; unsigned char tem[4]={0x01,0X02,0X03,0X04} ; memcpy((uint8_t*)&u32,tem,sizeof(tem)); printf("%04X\n",u32); u8_to_u32(tem,&u32); printf("%04X\n",u32); } //04030201 //04030201 如果上面修改if(1)就反過來