C言語バイト配列(バイトストリーム受信後)から16進文字列へ

1522 ワード

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
", dest); } /********************************************/ // 16 02 void ByteToHexStr02(BYTE *pbSrc, BYTE *pbDest, int nLen) { char ddl, ddh; int i; for (i = 0; i 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
", 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> 4); temp = pbSrc[i] & 0x0f; pbDest[2 * i + 1] = IntToHexChar(temp); } printf("out = %s
", pbDest); }