本文主要向大家介绍了C/C++知识点之C语言数据转换(hex - char - byte array - acsii),通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。网上的整理过来的,来源记不清了,感谢作者。
DataConvert.c
#include #include #include "DataConvert.h"
int strToHex(char *ch, char *hex)
{
int high,low;
int tmp = 0;
if(ch == NULL || hex == NULL){
return -1;
}
if(strlen(ch) == 0){
return -2;
}
while(*ch){
tmp = (int)*ch;
high = tmp >> 4;
low = tmp & 15;
*hex++ = valueToHexCh(high); //先写高字节
*hex++ = valueToHexCh(low); //其次写低字节
ch++;
}
*hex = ‘\0‘;
return 0;
}
int hexToStr(char *hex, char *ch)
{
int high,low;
int tmp =