C语言常用的转换工具函数有哪些
1、字符串转十六进制
代码实现:
void strtohex(char *pbdest, char *pbsrc, int nlen){ char h1,h2; char s1,s2; int i; for (i=0; i { h1 = pbsrc[2*i]; h2 = pbsrc[2*i+1]; s1 = toupper(h1) - 0x30; //toupper 转换为大写字母 if (s1 》 9) s1 -= 7; s2 = toupper(h2) - 0x30; if (s2 》 9) s2 -= 7; pbdest[i] = s1*16 + s2; }}
2、十六进制转字符串
代码实现:
void hextostr(char *pszdest, char *pbsrc, int nlen){ char ddl, ddh; for (int i = 0; i 《 nlen; i++) { ddh = 48 + pbsrc[i] / 16; ddl = 48 + pbsrc[i] % 16; if (ddh 》 57) ddh = ddh + 7; if (ddl 》 57) ddl = ddl + 7; pszdest[i * 2] = ddh; pszdest[i * 2 + 1] = ddl; } pszdest[nlen * 2] = ‘