C语言如何将十六进制转为十进制?
How to convert hexadecimal to decimal in C?
因此,凭借我在 C 方面的初学者水平,我一直在尝试编写一个代码,使用数组将十六进制输入转换为十进制。我相信您会通过查看我的代码更具体地了解它,但它无法正常工作。我一直收到比预期多得多的金额。
#include <stdio.h>
int main()
{
int i, k, j, N, power, result;
char array[50];
result = 0;
printf("how many charecters are there in your hexadecimal input?: \n");
scanf("%d", &N);
for(k=0; k<=N-1; k++)
{
printf("What is the %d . value of your hexadecimal input?: \n", k+1);
scanf("%s", &array[k]);
}
for(i=0; i<=N-1; i++)
{
power = 1;
for(j=0; j<=i; j++)
{
power = power *16;
}
if((array[i] >= 0) && (array[i] <= 9))
{
result = result + array[i] * power;
}
else
{
result = result + (array[i] - 'A' + 10) * power;
}
}
printf("your result is %d", result);
return 0;
}
你的代码过于复杂和错误,但思路是正确的。
你想要这个:
#include <stdio.h>
#include <string.h>
int main()
{
char array[50];
scanf("%49s", array); // just use a single scanf with %s
// "49s" will avoid buffer overflow of array
int length = strlen(array); // get length of string (and use meaningful
// variable name length instead of N)
int result = 0;
for (int i = 0; i < length; i++)
{
int nibble; // nibble is the hexadécimal "digit" from 0 to 15
if ((array[i] >= '0') && (array[i] <= '9'))
{
nibble = array[i] - '0';
}
else
{
nibble = array[i] - 'A' + 10;
}
result *= 16; // no need to maintain power, just multiply by 16
// on each run
result += nibble; // ... and add the nibble
}
printf("your result is %d", result);
return 0;
}
仍有改进空间:
- 你应该接受小写字母
- 没有对'G'、'H'等无效字符进行测试
- 你应该将转换代码放在函数中,例如
int HexStringToDecimal(const char *hexstring)
- 当然还有很多我忘记的其他事情。
为您练习:转换该代码,使其转换为 十进制 字符串而不是 十六进制 字符串。提示:代码会更简单。
旁注:
避免这样的构造:
for(k=0; k<=N-1; k++)
而是使用这个更易读的完全等价物:
for(k=0; k<N; k++)
只是稍微编辑了你的代码,
而且输入应该是从左到右
而不是我认为您已经编码的从右到左。
因此,对于 C6,首先是 C,然后是 6。
#include <stdio.h>
int main()
{
int i, k, j, N, power, result;
char array[50];
result = 0;
printf("how many charecters are there in your hexadecimal input?: \n");
scanf("%d", &N);
for(k=0; k<=N-1; k++)
{
printf("What is the %d . value of your hexadecimal input?: \n", k+1);
scanf(" %c", &array[k]);
}
for(i=0; i<=N-1; i++)
{
power = 1;
for(j=0; j<=N-2-i; j++)
{
power = power*16;
}
if((array[i] >= '0') && (array[i] <= '9'))
{
result = result + (array[i] - '0') * power;
}
else
{
result = result + (array[i] - 'A' + 10) * power;
}
}
printf("your result is %d", result);
return 0;
}
外卖
- 您的输入效率低下。并且在 scanf 中也是 %c 而不是 %s。
if((array[i] >= 0) && (array[i] <= 9))
,假设 i=1 和 array[1] = 7。现在,您正在比较 7 的 ascii 值,即 55,而不是 7。
- 在 if 的语句中同样的事情,正在使用 array[i] 的 ascii 值,所以我相应地减去。
因此,凭借我在 C 方面的初学者水平,我一直在尝试编写一个代码,使用数组将十六进制输入转换为十进制。我相信您会通过查看我的代码更具体地了解它,但它无法正常工作。我一直收到比预期多得多的金额。
#include <stdio.h>
int main()
{
int i, k, j, N, power, result;
char array[50];
result = 0;
printf("how many charecters are there in your hexadecimal input?: \n");
scanf("%d", &N);
for(k=0; k<=N-1; k++)
{
printf("What is the %d . value of your hexadecimal input?: \n", k+1);
scanf("%s", &array[k]);
}
for(i=0; i<=N-1; i++)
{
power = 1;
for(j=0; j<=i; j++)
{
power = power *16;
}
if((array[i] >= 0) && (array[i] <= 9))
{
result = result + array[i] * power;
}
else
{
result = result + (array[i] - 'A' + 10) * power;
}
}
printf("your result is %d", result);
return 0;
}
你的代码过于复杂和错误,但思路是正确的。
你想要这个:
#include <stdio.h>
#include <string.h>
int main()
{
char array[50];
scanf("%49s", array); // just use a single scanf with %s
// "49s" will avoid buffer overflow of array
int length = strlen(array); // get length of string (and use meaningful
// variable name length instead of N)
int result = 0;
for (int i = 0; i < length; i++)
{
int nibble; // nibble is the hexadécimal "digit" from 0 to 15
if ((array[i] >= '0') && (array[i] <= '9'))
{
nibble = array[i] - '0';
}
else
{
nibble = array[i] - 'A' + 10;
}
result *= 16; // no need to maintain power, just multiply by 16
// on each run
result += nibble; // ... and add the nibble
}
printf("your result is %d", result);
return 0;
}
仍有改进空间:
- 你应该接受小写字母
- 没有对'G'、'H'等无效字符进行测试
- 你应该将转换代码放在函数中,例如
int HexStringToDecimal(const char *hexstring)
- 当然还有很多我忘记的其他事情。
为您练习:转换该代码,使其转换为 十进制 字符串而不是 十六进制 字符串。提示:代码会更简单。
旁注:
避免这样的构造:
for(k=0; k<=N-1; k++)
而是使用这个更易读的完全等价物:
for(k=0; k<N; k++)
只是稍微编辑了你的代码, 而且输入应该是从左到右 而不是我认为您已经编码的从右到左。 因此,对于 C6,首先是 C,然后是 6。
#include <stdio.h>
int main()
{
int i, k, j, N, power, result;
char array[50];
result = 0;
printf("how many charecters are there in your hexadecimal input?: \n");
scanf("%d", &N);
for(k=0; k<=N-1; k++)
{
printf("What is the %d . value of your hexadecimal input?: \n", k+1);
scanf(" %c", &array[k]);
}
for(i=0; i<=N-1; i++)
{
power = 1;
for(j=0; j<=N-2-i; j++)
{
power = power*16;
}
if((array[i] >= '0') && (array[i] <= '9'))
{
result = result + (array[i] - '0') * power;
}
else
{
result = result + (array[i] - 'A' + 10) * power;
}
}
printf("your result is %d", result);
return 0;
}
外卖
- 您的输入效率低下。并且在 scanf 中也是 %c 而不是 %s。
if((array[i] >= 0) && (array[i] <= 9))
,假设 i=1 和 array[1] = 7。现在,您正在比较 7 的 ascii 值,即 55,而不是 7。- 在 if 的语句中同样的事情,正在使用 array[i] 的 ascii 值,所以我相应地减去。