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)
{
return0;
}
unsigned int uiKeySize =strlen(pUserInput)/2;
int i =0;
char cTempor =0;
c语言如何创建字符串数组while(i < uiKeySize)
{
if(*pUserInput >='0'&&*pUserInput <='9')
{
cTempor =*pUserInput -48;
}
else
{
cTempor =0xa+(*pUserInput -'a');
}
pKeyArray[i]= cTempor;
pUserInput++;
if(*pUserInput >='0'&&*pUserInput <='9')
{
cTempor =*pUserInput -48;
}
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("");
return0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论