C语言:字符串转为16进制

#include 
#include 
#include 
/*
// C prototype : void StrToHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
// parameter(s): [OUT] pbDest - 输出缓冲区
// [IN] pbSrc - 字符串
// [IN] nLen - 16进制数的字节数(字符串的长度/2)
// return value:
// remarks : 将字符串转化为16进制数
*/
void StrToHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
{
	char h1,h2;
	unsigned char s1,s2;
	int i;

	for (i=0; i 9)
				s1 -= 7;

			s2 = toupper(h2) - 0x30;
			if (s2 > 9)
				s2 -= 7;

			pbDest[i] = s1*16 + s2;
		}
}




int main()
{
	unsigned char tmp[64] = "00000000000c0000015748db06ca358d000c00000000190513171846";
	unsigned char out[32] = {0};
	memset(out, 0 ,8);
	
	
	StrToHex(out, tmp, 28);
	
	
	int i;
	for(i=0; i

测试:

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# gcc -o test test.c
root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test
out[0] : 0 
out[1] : 0 
out[2] : 0 
out[3] : 0 
out[4] : 0 
out[5] : c 
out[6] : 0 
out[7] : 0 
out[8] : 1 
out[9] : 57 
out[10] : 48 
out[11] : db 
out[12] : 6 
out[13] : ca 
out[14] : 35 
out[15] : 8d 
out[16] : 0 
out[17] : c 
out[18] : 0 
out[19] : 0 
out[20] : 0 
out[21] : 0 
out[22] : 19 
out[23] : 5 
out[24] : 13 
out[25] : 17 
out[26] : 18 
out[27] : 46 
out[28] : 0 
out[29] : 0 
out[30] : 0 
out[31] : 0 
root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# 

参考链接: https://blog.csdn.net/u013776495/article/details/51318031

程序是抄人家的。

你可能感兴趣的:(C/C++)