您需要同时使用2(十六进制)字符…然后计算int值
之后,使char转换像…
char d =(char)intValue;
对于十六进制字符串中的每个2chars来执行此操作
如果字符串字符只有0-9A-F,则这样做:
#include
#include
int hex_to_int(char c){
int first = c / 16 - 3;
int second = c % 16;
int result = first*10 + second;
if(result > 9) result--;
return result;
}
int hex_to_ascii(char c,char d){
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
return high+low;
}
int main(){
const char* st = "48656C6C6F3B";
int length = strlen(st);
int i;
char buf = 0;
for(i = 0; i < length; i++){
if(i % 2 != 0){
printf("%c",hex_to_ascii(buf,st[i]));
}else{
buf = st[i];
}
}
}