int float寫入位元組資料各位元組序的實現

2021-09-29 07:43:42 字數 3983 閱讀 8064

int與位元組陣列轉換比較簡單,用變數位與oxff等和移位即可實現,因為變數位與0xff等已經不受計算機cpu大小端序的影響,可直接實現,如:

#define byte0_littleend(val) static_cast((val) & 0xff)

#define byte1_littleend(val) static_cast(((val) & 0xff00) >> 8)

#define byte2_littleend(val) static_cast(((val) & 0xff0000) >> 16)

#define byte3_littleend(val) static_cast(((val) & 0xff000000) >> 24)

#define byte4_littleend(val) static_cast(((val) & 0xff00000000) >> 32)

#define byte5_littleend(val) static_cast(((val) & 0xff0000000000) >> 40)

#define byte6_littleend(val) static_cast(((val) & 0xff000000000000) >> 48)

#define byte7_littleend(val) static_cast(((val) & 0xff00000000000000) >> 56)

#define byte0_bigend(val) static_cast(((val) & 0xff00000000000000) >> 56)

#define byte1_bigend(val) static_cast(((val) & 0xff000000000000) >> 48)

#define byte2_bigend(val) static_cast(((val) & 0xff0000000000) >> 40)

#define byte3_bigend(val) static_cast(((val) & 0xff00000000) >> 32)

#define byte4_bigend(val) static_cast(((val) & 0xff000000) >> 24)

#define byte5_bigend(val) static_cast(((val) & 0xff0000) >> 16)

#define byte6_bigend(val) static_cast(((val) & 0xff00) >> 8)

#define byte7_bigend(val) static_cast((val) & 0xff)

#define uint8_2_uint16_littleend(byte0, byte1) static_cast((((byte1) & 0xff) << 8) + ((byte0) & 0xff))

#define uint8_2_uint16_bigend(byte0, byte1) static_cast((((byte0) & 0xff) << 8) + ((byte1) & 0xff))

#define uint8_2_uint32_littleend(byte0, byte1, byte2, byte3) static_cast((((byte3) & 0xff) << 24) + (((byte2) & 0xff) << 16) + (((byte1) & 0xff) << 8) + ((byte0) & 0xff))

#define uint8_2_uint32_bigend(byte0, byte1, byte2, byte3) static_cast((((byte0) & 0xff) << 24) + (((byte1) & 0xff) << 16) + (((byte2) & 0xff) << 8) + ((byte3) & 0xff))

但是float存放用位與0xff和移位會有精度丟失的問題,需要用記憶體操作實現,可擴充套件性的實現是需要動態判斷計算機的大小端序,**如下:

/*******************************************

* 功能:判斷cpu是否為小端序

* 返回值:

* bool:判斷結果,true -- 小端序,false -- 大端序

* *****************************************/

bool iscpulittleend()

endian;

endian.ival = 1;

return (1 == endian.cval);

}

float資料分別按大小端寫入位元組陣列**如下:

/*******************************************

* 功能:float資料按小端序寫入位元組陣列

* 輸入引數:

* fval:待轉換的資料

* 輸出引數:

* pdata:寫入float資料的小端序位元組陣列,空間至少可以存放4位元組

* *****************************************/

void float2arrbylittleend(const float fval, char *pdata)

; memcpy(szdata, &fval, sizeof(szdata));

if (iscpulittleend())

else

return;

}/*******************************************

* 功能:float資料按大端序寫入位元組陣列

* 輸入引數:

* fval:待轉換的資料

* 輸出引數:

* pdata:寫入float資料的大端序位元組陣列,空間至少可以存放4位元組

* *****************************************/

void float2arrbybigend(const float fval, char *pdata)

; memcpy(szdata, &fval, sizeof(szdata));

if (iscpulittleend())

else

return;

}

位元組陣列按大小端序寫回float資料的**如下:

/*******************************************

* 功能:小端序位元組陣列轉float資料

* 輸入引數:

* pdata:小端序的位元組陣列

* 返回值:

* float:轉換資料

* *****************************************/

float arr2floatbylittleend(const char *pdata)

else

; for (int i = 0; i < 4; ++i)

memcpy(&fval, sztemp, 4);

} }return fval;

}/*******************************************

* 功能:大端序位元組陣列轉float資料

* 輸入引數:

* pdata:大端序的位元組陣列

* 返回值:

* float:轉換資料

* *****************************************/

float arr2floatbybigend(const char *pdata)

; for (int i = 0; i < 4; ++i)

memcpy(&fval, sztemp, 4);

} else

}return fval;

}

其餘int、double、short等型別理用記憶體操作實現方法類似,只是相關變數型別變化為對應型別即可。

各變數位元組數

win64,也就是x64編譯配置下 char 1位元組 short 2位元組 int 4位元組 long 4位元組 long long 8位元組 float 4位元組 double 8位元組 long double 8位元組 wchar t 2位元組 bool 1位元組 指標都是8位元組 char ...

資料型別位元組數

一 程式執行平台 不同的平台上對不同資料型別分配的位元組數是不同的。個人對平台的理解是cpu os compiler,是因為 1 64位機器也可以裝32位系統 x64裝xp 2 32位機器上可以有16 32位的編譯器 xp上有tc是16位的,其他常見的是32位的 3 即使是32位的編譯器也可以弄出6...

32位和64位各變數位元組數

資料型別編譯配置 x86x64 char 1字元1字元 short 2字元2字元 int4字元 4字元long 4字元4字元 longlong 8字元8字元 float 4字元4字元 double 8字元8字元 long double 8字元8字元 wchar t 2字元2字元 bool 1字元1字...