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

左手的ㄟ右手 2022-03-09 05:25 731阅读 0赞

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

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

  1. unsigned int my_func(char *pUserInput, unsigned char *pKeyArray)
  2. {
  3. if (NULL == pUserInput || NULL == pKeyArray)
  4. {
  5. return 0;
  6. }
  7. unsigned int uiKeySize = strlen(pUserInput) / 2;
  8. int i = 0;
  9. char cTempor = 0;
  10. while(i < uiKeySize)
  11. {
  12. if (*pUserInput >= '0' && *pUserInput <= '9')
  13. {
  14. cTempor = *pUserInput - 48;
  15. }
  16. else
  17. {
  18. cTempor = 0xa + (*pUserInput - 'a');
  19. }
  20. pKeyArray[i] = cTempor;
  21. pUserInput++;
  22. if (*pUserInput >= '0' && *pUserInput <= '9')
  23. {
  24. cTempor = *pUserInput - 48;
  25. }
  26. else
  27. {
  28. cTempor = 0xa + (*pUserInput - 'a');
  29. }
  30. pKeyArray[i] = (pKeyArray[i] << 4) | cTempor;
  31. pUserInput++;
  32. i++;
  33. }
  34. return uiKeySize;
  35. }
  36. int main()
  37. {
  38. char cArr[] = "41a65db35c069fbfc412be4f73223e007332ba32cc3768a23d9f971960aa6744"; //字符个数双数,小写
  39. unsigned char ucBrr[32];
  40. int i;
  41. persist_ssl_hashKeyConvert(cArr, ucBrr);
  42. printf("cArr = %s\n", cArr);
  43. printf("ucBrr =\n");
  44. for (i = 0; i < sizeof(ucBrr); i++)
  45. {
  46. printf("%x", ucBrr[i]>>4);
  47. printf("%x", ucBrr[i] & 0x0f);
  48. printf(" ");
  49. }
  50. puts("");
  51. return 0;
  52. }

发表评论

表情:
评论列表 (有 0 条评论,731人围观)

还没有评论,来说两句吧...

相关阅读