C 语言 字节数组(字节流接收后)转为16进制字符串

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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值