请问这个十六进制转十进制为啥不对
#include<stdio.h>#include<math.h>
#include<string.h>
#include<stdlib.h>
char final_1;
int q = 0;
int exchage2(char ch)
{
int h=0;
switch (ch)
{
case '0':
h = 0; break;
case '1':
h = 1; break;
case '2':
h = 2; break;
case '3':
h = 3; break;
case '4':
h = 4; break;
case '5':
h = 5; break;
case '6':
h = 6; break;
case '7':
h= 7; break;
case '8':
h = 8; break;
case '9':
h = 9; break;
case'A':
h = 10; break;
case'B':
h = 11; break;
case'C':
h = 12; break;
case'D':
h = 13; break;
case'E':
h = 14; break;
case'F':
h = 15; break;
}
return h;
}
void delesomething(char arr[])
{
for (int i = 0; i < strlen(arr); i++)
{
if (arr <= '9' && arr>='0')
final_1 = arr;
else if (arr >= 'A' && arr <= 'F')
{
final_1 = arr;
}
else if (arr >= 'a' && arr <= 'f')
{
final_1 = arr + 'A' - 'a';
}
}
}
intexchange(char arr[])
{
long long unsigned sum = 0;
for (int i = strlen(arr) - 1; i >= 0; i--)
{
int b = strlen(arr) - i - 1;
sum += exchage2(arr)*pow(16,b);
}
return sum;
}
int main()
{
char arr;
gets(arr);
delesomething(arr);
printf("十六进制0x%s\n", final_1);
long long unsigned ar=exchange(final_1);
printf("十进制%lld\n", ar);
return 0;
}
在数比较小的时候转换成功,数比较大就不成功了 本帖最后由 jhq999 于 2021-11-18 20:04 编辑
把int类型的变量和函数类型都换成unsigned long long类型;
printf("十进制%llu\n", ar);
页:
[1]