十进制整数、浮点数与十六进制字符串互转
/** * 函数:Hex2Dec * 描述:十六进制字符串转十进制整数 * 参数:pstrHex 十六进制字符串(以数值的字节顺序) * nLen 字符串长度(不含NULL结束符) * 返回:十进制整数 */ int Hex2Int(unsigned char *pstrHex, int nLen) { int i, j = 0; int nRet = 0; unsigned char* pTmp = (unsigned char*)(&nRet); for (i = 0; i <= nLen - 2; i = i + 2) { if ((toupper(pstrHex[i]) >= 'A')) {// A--D // 16进制中A和0相差16,但ASCII码中A和0不是相差16 pTmp[j] = ((toupper(pstrHex[i]) - 'A') + 10) << 4; } else {// 0--9 pTmp[j] = (pstrHex[i] - '0') << 4; } if ((toupper(pstrHex[i+1]) >= 'A')) { pTmp[j] = pTmp[j] | ((toupper(pstrHex[i + 1]) - 'A') + 10); } else { pTmp[j] = pTmp[j] | (pstrHex[i + 1] - '0'); } j++; } return nRet; } /** * 函数:Hex2Float * 描述:十六进制字符串转浮点数 * 参数:pstrHex 十六进制字符串(以数值的字节顺序) * nLen 字符串长度(不含NULL结束符) * 返回:浮点数 */ float Hex2Float(unsigned char *pstrHex, int nLen) { int i, j = 0; float fRet = 0; unsigned char* pTmp = (unsigned char*)(&fRet); for (i = 0; i <= nLen - 2; i = i + 2) { if ((toupper(pstrHex[i]) >= 'A')) {// A--D // 16进制中A和0相差16,但ASCII码中A和0不是相差16 pTmp[j] = ((toupper(pstrHex[i]) - 'A') + 10) << 4; } else {// 0--9 pTmp[j] = (pstrHex[i] - '0') << 4; } if ((toupper(pstrHex[i+1]) >= 'A')) { pTmp[j] = pTmp[j] | ((toupper(pstrHex[i + 1]) - 'A') + 10); } else { pTmp[j] = pTmp[j] | (pstrHex[i + 1] - '0'); } j++; } return fRet; } /** * 函数:Int2Hex * 描述:获取十进制整数的十六进制字符串表示(以本机内存的字节存储顺序) * 参数:nDec 待转换整数 * pstrBuf 字符串缓冲区,存储转换结果,转换结果呈现的是数值在本机内存中高低字节的存储顺序,根据本机大小端模式的不同, * 呈现的不一定是该数值的字节顺序(大端是一样的,而小端是逆序的)。在windows下,由于是小端,如果想要pstrBuf返回 * 数值的字节顺序,需要首先把nDec转换为大端再传入。 * 返回:字符串缓冲区首地址 */ unsigned char* Int2Hex(int nDec, unsigned char *pstrBuf) { int i; for (i = 0; i < 8; i++) { int t = nDec >> (4 * (7 - i)); t = t & 0x0000000F; switch (t) { case 0: pstrBuf[i] = '0'; break; case 1: pstrBuf[i] = '1'; break; case 2: pstrBuf[i] = '2'; break; case 3: pstrBuf[i] = '3'; break; case 4: pstrBuf[i] = '4'; break; case 5: pstrBuf[i] = '5'; break; case 6: pstrBuf[i] = '6'; break; case 7: pstrBuf[i] = '7'; break; case 8: pstrBuf[i] = '8'; break; case 9: pstrBuf[i] = '9'; break; case 10: pstrBuf[i] = 'A'; break; case 11: pstrBuf[i] = 'B'; break; case 12: pstrBuf[i] = 'C'; break; case 13: pstrBuf[i] = 'D'; break; case 14: pstrBuf[i] = 'E'; break; case 15: pstrBuf[i] = 'F'; break; default: break; } } return pstrBuf; } /** * 函数:Float2Hex * 描述:获取浮点数的十六进制字符串表示(以本机内存的字节存储顺序) * 参数:fDec 待转换浮点数 * pstrBuf 字符串缓冲区,存储转换结果,转换结果呈现的是数值在本机内存中高低字节的存储顺序,根据本机大小端模式的不同, * 呈现的不一定是该数值的字节顺序(大端是一样的,而小端是逆序的)。在windows下,由于是小端,如果想要pstrBuf返回 * 数值的字节顺序,需要首先把fDec转换为大端再传入。 * 返回:字符串缓冲区首地址 */ unsigned char* Float2Hex(float fDec, unsigned char *pstrBuf) { int i; int* pIVal = (int*)&fDec; for (i = 0; i < 8; i++) { int t = *pIVal >> (4 * (7 - i)); t = t & 0x0000000F; switch (t) { case 0: pstrBuf[i] = '0'; break; case 1: pstrBuf[i] = '1'; break; case 2: pstrBuf[i] = '2'; break; case 3: pstrBuf[i] = '3'; break; case 4: pstrBuf[i] = '4'; break; case 5: pstrBuf[i] = '5'; break; case 6: pstrBuf[i] = '6'; break; case 7: pstrBuf[i] = '7'; break; case 8: pstrBuf[i] = '8'; break; case 9: pstrBuf[i] = '9'; break; case 10: pstrBuf[i] = 'A'; break; case 11: pstrBuf[i] = 'B'; break; case 12: pstrBuf[i] = 'C'; break; case 13: pstrBuf[i] = 'D'; break; case 14: pstrBuf[i] = 'E'; break; case 15: pstrBuf[i] = 'F'; break; default: break; } } return pstrBuf; } // 旋转十六进制的字符串顺序 void RotateHexString32(unsigned char *pstrBuf) { unsigned char szTmp[2]; memcpy(szTmp, pstrBuf, 2); memcpy(pstrBuf, pstrBuf+6, 2); memcpy(pstrBuf+6, szTmp, 2); memcpy(szTmp, pstrBuf+2, 2); memcpy(pstrBuf+2, pstrBuf+4, 2); memcpy(pstrBuf+4, szTmp, 2); }
void Short2Hex(unsigned short dec, char *hex)
{
unsigned short d = dec;
byte r;
byte i = 3;
memset(hex, '0', 4);
while (d != 0)
{
r = d % 16;
d = d / 16;
if (r < 10)
{
r += 48;
}
else
{
r = (r - 10) + 97;
}
hex[i--] = r;
}
}
unsigned short Hex2Short(char *hex)
{
unsigned short m = 1;
unsigned short sum = 0;
byte i = 4;
char *p = hex + 3;
while (i > 0)
{
byte b;
if (*p >= 48 && *p <= 57)
b = *p - 48;
else if (*p >= 65 && *p <= 70)
b = *p - 65 + 10;
else if (*p >= 97 && *p <= 102)
b = *p - 97 + 10;
sum += b * m;
m *= 16;
i--;
p--;
}
return sum;
}