BYTE
c++ BYTE相关操作。
字符串转BYTE(“0x14”->0x14)
下面的函数为MFC中获取控件中的输入文字,将其转换为对应的16进制BYTE
void GetHexFromStr(CWnd *wd, BYTE *nByte){CString strText;wd->GetWindowText(strText);int nTmpNum = 0;sscanf(strText, "%x", &nTmpNum);*nByte = nTmpNum;}
BYTE 转字符串(0x14 -> “0x14”)
void HexToStr(const BYTE bData, char *szBuff){sprintf(szBuff, "0x%02x", bData);}
//调用如下char szBuffer[20] = {0 };HexToStr(0x15, szBuffer);
位转BYTE(0x12 ->00010010)
void GetBitFromByte(const BYTE byData, int *bitList){for (int i =0 ;i<8;++i){int nTmp = (1 << i);bitList[i] = ((byData & nTmp) == nTmp )? 1 : 0;}}
//调用时int bitList[8] = {0 };GetBitFromByte(0x12, bitList);
BYTE转位(00010010 -> 0x12)
void BitListToByte(BYTE *bData,const int *bitList){int n = 0;for (int i = 0 ; i < 8;++i){int nB = bitList[i] * (1 << i);n += nB;}*bData = n;}
int 转 BYTE[2]
static void intToByte2(int i, BYTE *bytes){bytes[0] = (byte)(0xff & i);bytes[1] = (byte)((0xff00 & i) >> 8);}
BYTE[2] 转int
static int Byte2ToInt(BYTE *bytes){int num = bytes[0] & 0xFF;num |= ((bytes[1] << 8) & 0xFF00);return num;}
如果觉得《c++ BYTE相关操作(字符串与BYTE BYTE转换为8位的数组 int 转换 BYTE)》对你有帮助,请点赞、收藏,并留下你的观点哦!