字符字符串转十六进制字符串
void StringToHex(char *in,char *out)
{
int high,low;
while(*in)
{
high=(*in)>>4;
low=(*in)&0xF;
*(out++)=high>9 ? high-10+'a' : high+'0';
*(out++)=low>9 ? low-10+'a' : low+'0';
in++;
}
*out='\0';
}
// 十六进制字符串转字符字符串
int hexCharToValue(const char ch){
int result = 0;
//获取16进制的高字节位数据
if(ch >= '0' && ch <= '9'){
result = (int)(ch - '0');
}
else if(ch >= 'a' && ch <= 'z'){
result = (int)(ch - 'a') + 10;
}
else if(ch >= 'A' && ch <= 'Z'){
result = (int)(ch - 'A') + 10;
}
else{
result = -1;
}
return result;
}
int hexToStr(char *hex, char *ch)
{
int high,low;
int tmp = 0;
if(hex == NULL || ch == NULL){
return -1;
}
if(strlen(hex) %2 == 1){
return -2;
}
while(*hex){
high = hexCharToValue(*hex);
if(high < 0){
*ch = '\0';
return -3;
}
hex++; //指针移动到下一个字符上
low = hexCharToValue(*hex);
if(low < 0){
*ch = '\0';
return -3;
}
tmp = (high << 4) + low;
*ch++ = (char)tmp;
hex++;
}
*ch = '\0';
return 0;
}