一、将数组转换为十六进制同值的字符串
读取数组中的数字,打印成字符串的时候以2位大写的格式。
1 int arrayToStr(unsigned char *buf, unsigned int buflen, char *out)
2 {
3 char strBuf[33] = {0};
4 char pbuf[32];
5 int i;
6 for(i = 0; i < buflen; i++)
7 {
8 sprintf(pbuf, "%02X", buf[i]);
9 strncat(strBuf, pbuf, 2);
10 }
11 strncpy(out, strBuf, buflen * 2);
12 printf("out = %s\n", out);
13 return buflen * 2;
14 }
二、将十六进制的字符串转换为十六进制数组
下面定义的字符串中的字符只能是0-F的字符,但是不区分大小写的,前面是安装两位为一个数字进行转换,最后一个数字如果还是两位的则正常转换,如果只剩一位的话则在前面补零输出。
三、补充:将十进制字符串转化为十进制数组
1 int StringToCom(char *str, unsigned char *out, int *outlen)
2 {
3 char *p = str;
4 char high = 0, low = 0;
5 int tmplen = strlen(p), cnt = 0;
6 tmplen = strlen(p);
7 if(tmplen % 2 != 0) return -1;
8 while(cnt < tmplen / 2) //1213141516171819
9 {
10 out[cnt] = (*p - 0x30) * 10 + (*(++p) - 0x30);
11 p++;
12 cnt ++;
13 }
14 *outlen = tmplen / 2;
15 return tmplen / 2;
16 }
参考:https://www.cnblogs.com/songshuaiStudy/p/10531139.html