C语言字符串转换为十六进制字符数组

要求:
char cArr[20] = “a1b2c3d4e5f6”; //字符个数双数
char cBrr[6] = {0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf6};

调用函数,参数1传入cArr, 参数2传入cBrr

unsigned int my_func(char *pUserInput, unsigned char *pKeyArray)
{
	if (NULL == pUserInput || NULL == pKeyArray)
	{
			return 0;
	}
	
	unsigned int uiKeySize = strlen(pUserInput) / 2;
	int i = 0;
	char cTempor = 0;
	
	while(i < uiKeySize)
	{
		if (*pUserInput >= '0' && *pUserInput <= '9')
		{
			cTempor = *pUserInput - 48;
		}
		else if (*pUserInput >= 'a' && *pUserInput <= 'z') 
		{
			cTempor = 0xa + (*pUserInput - 'a');
		}
		else 
		{
			cTempor = 0xa + (*pUserInput - 'A');
		}
		
		pKeyArray[i] = cTempor;
		pUserInput++;
		
		if (*pUserInput >= '0' && *pUserInput <= '9')
		{
			cTempor = *pUserInput - 48;
		}
		else if (*pUserInput >= 'a' && *pUserInput <= 'z') 
		{
			cTempor = 0xa + (*pUserInput - 'a');
		}
		else 
		{
			cTempor = 0xa + (*pUserInput - 'A');
		}
		
		pKeyArray[i] = (pKeyArray[i] << 4) | cTempor;
		pUserInput++;
		i++;
	}
	
	return uiKeySize;	
}

int main()
{
	char cArr[] = "41a65db35c069fbfc412be4f73223e007332ba32cc3768a23d9f971960aa6744";  //字符个数双数,小写
	unsigned char ucBrr[32];
	int i;
	
	my_func(cArr, ucBrr);
	
	printf("cArr = %s\n", cArr);
	printf("ucBrr =\n");
	for (i = 0; i < sizeof(ucBrr); i++)
	{
		printf("%x", ucBrr[i]);
		printf(" ");
	}
	puts("");
  
	return 0;
}
  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值