展开全部
我重新写了e69da5e887aa62616964757a686964616f31333335343964个,先把两个数转为 10 进制,算完再转为 16 进制。完整代码如下:#include
#include
#include
//进制转换
int hex2int(char *str, int *result){
*result = 0;
while(*str > 0){
if(*str >= '0' && *str <= '9'){
*result = *result * 16 + *str++ - '0';
}else if(*str >= 'a' && *str <= 'f'){
*result = *result * 16 + *str++ - 'a' + 10;
}else if(*str >= 'A' && *str <= 'F'){
*result = *result * 16 + *str++ - 'A' + 10;
}else{
return 1;
}
}
return 0;
}
int int2num(int num, char **result, char scale){
char arr[32];
char *temp = arr + 32;
char *table = "0123456789abcdef";
*--temp = 0;
while(num > scale){
*--temp = table[num % scale];
num = num / scale;
}
if(num){
*--temp = table[num];
}else if(*temp == 0){
*--temp = '0';
}
*result = strdup(temp);
}
char* ADD(char *x, char *y){
int a, b;
if(hex2int(x, &a)){
printf("x isn't hex.\n");
}
if(hex2int(y, &b)){
printf("y isn't hex.\n");
}
int2num(a + b , &x, 16);
return x;
}
int main(){
char x[32], y[32];
printf("x: ");
scanf("%s", x);
printf("y: ");
scanf("%s", y);
char *t = ADD(x, y);
printf("x + y = %s\n", t);
free(t);//释放内存
return 0;
}