https://blog.csdn.net/qq387732471/article/details/7360988
有好几个版本,下面一一列出:
//字节数组转为16进制字符串01
void ByteToHexStr01(const unsigned char* source, char* dest, int sourceLen)
{
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i++)
{
highByte = source[i] >> 4;
lowByte = source[i] & 0x0f;
highByte += 0x30;
if (highByte > 0x39)
dest[i * 2] = highByte + 0x07;
else
dest[i * 2] = highByte;
lowByte += 0x30;
if (lowByte > 0x39)
dest[i * 2 + 1] = lowByte + 0x07;
else
dest[i * 2 + 1] = lowByte;
}
printf("out = %s\n", dest);
}
/********************************************/
//字节数组转为16进制字符串02
void ByteToHexStr02(BYTE *pbSrc, BYTE *pbDest, int nLen)
{
char ddl, ddh;
int i;
for (i = 0; i<nLen; i++)
{
ddh = 48 + pbSrc[i] / 16;
ddl = 48 + pbSrc[i] % 16;
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
pbDest[i * 2] = ddh;
pbDest[i * 2 + 1] = ddl;
}
pbDest[nLen * 2] = '\0';
printf("out = %s\n", pbDest);
}
/********************************************/
//字节数组转为16进制字符串03
unsigned char IntToHexChar(unsigned char c)
{
if (c > 9)
return (c + 55);
else
return (c + 0x30);
}
void ByteToHexStr03(BYTE *pbSrc, BYTE *pbDest, int nLen)
{
int i;
unsigned char temp;
for (i = 0; i<nLen; i++)
{
temp = pbSrc[i] & 0xf0;
pbDest[2 * i] = IntToHexChar(temp >> 4);
temp = pbSrc[i] & 0x0f;
pbDest[2 * i + 1] = IntToHexChar(temp);
}
printf("out = %s\n", pbDest);
}