C语言 如何在C中制作十进制到十六进制转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20047598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:19:58  来源:igfitidea点击:

How to make a decimal to hexadecimal converter in C

chexdecimal

提问by user3004619

So I am an absolute beginner in C and I have to make a decimal to hexadecimal converter.

所以我是 C 的绝对初学者,我必须制作一个十进制到十六进制的转换器。

So I guess I would need to make a loop that loops until the result is 0.

所以我想我需要做一个循环,直到结果为 0。

But how do I make it remember all the remainders? The number is going to be input with scanf so I can't tailor the code to it.

但是我如何让它记住所有的余数?该数字将通过 scanf 输入,因此我无法为其定制代码。

Now I would want to do something like this

现在我想做这样的事情

while(number(n)!=0)
{
    number0 / 16 = number1
    number0 % 16 = remainder0
    number1 / 16 = number2
    number1 % 16 = remainder1
    .....
    number(n-1) / 16 = 0
    number(n-1) % 16 = lastremainder
}

hex = lastremainder, ..., remainder2, remainder1, remainder0

But how can I make the program create variables during the loop? Do I have to use a complete different method? I took a look at other decimal to hex converters and I don't quite get how they work.

但是如何让程序在循环期间创建变量?我必须使用完全不同的方法吗?我查看了其他十进制到十六进制转换器,但我不太明白它们是如何工作的。

Like I said I am an absolute beginner so sorry if the question is stupid.

就像我说的那样,我是一个绝对的初学者,如果问题很愚蠢,那么抱歉。

Thank you for the replies. So arrays are the answer to my problem? I don't fully understand them right now but thank you for the point in the right direction.

感谢您的答复。那么数组是我问题的答案吗?我现在不完全理解它们,但感谢您指出正确方向。

回答by Elias Van Ootegem

I'd simply use sprintf, to be honest:

sprintf老实说,我只是简单地使用:

char hex[10];
sprintf(&hex, "%x", INT_MAX);//INT_MAX macro requires limits.h, though...
printf("%s\n", hex);//prints 7fffffff

Job done...
In full, your code could look something like:

工作完成...
总的来说,您的代码可能如下所示:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char hex[10];
    int input;
    scanf("%d", &input);
    sprintf(&hex, "%x", input);
    printf("The number %d, turned to hexadecimal is: %s\n", input, hex);
    return 0;
}

Or even:

甚至:

    int input;
    scanf("%d", &input);
    printf("The number %d, turned to hexadecimal is: %x\n", input, input);

回答by Eutherpy

Make an array and write the remains in it:

创建一个数组并将剩余部分写入其中:

int array[50];
int counter=0;

[...]

while(number!=0)
{
   [...]

   array[counter]=number%16;
   number/=16;
   ++counter;
}

The line array[counter]=number%16;means that the first element in the array will be number%16 - the second will be (number/16)%16 etc.

该行array[counter]=number%16;表示数组中的第一个元素将是 number%16 - 第二个将是 (number/16)%16 等。

You need the counterto know how many elements there is in the array (how much remains), so that you can later write them backwards.

您需要counter知道数组中有多少元素(剩余多少),以便稍后可以将它们向后写入。

(Take into consideration here that you have a limit - int array[50];because, what happens if your number is really big and you have more than 50 remains? The solution would be to write this dynamically, but I don't think you should worry about that at this point.)

(在这里考虑到你有一个限制——int array[50];因为,如果你的数字真的很大并且你有超过 50 个剩余的会发生什么?解决方案是动态地写这个,但我认为你不应该担心这个这点。)

回答by MeNa

If you want to be able to get size unlimited number, you need to use dynamic array. firstly you get the input-length by loop, and then allocate array as needed. you need to increase an index in your loop and put the current value in array[index].

如果您希望能够获得大小不受限制的数字,则需要使用动态数组。首先通过循环获取输入长度,然后根据需要分配数组。您需要在循环中增加一个索引并将当前值放入 array[index]。

回答by Kaushick Gope

Just try this:

试试这个:

    char hex = [], finalHex = [];
while(num != 0) {
num = num/16;
rem = num%16;
switch(rem) {
case 0 : 
hex = '0';
break;
case 1 : 
hex = '1';
break;
case 2 : 
hex = '2';
break;
case 3 : 
hex = '3';
break;
case 4 : 
hex = '4';
break;
case 5 : 
hex = '5';
break;
case 6 : 
hex = '6';
break;
case 7 : 
hex = '7';
break;
case 8 : 
hex = '8';
break;
case 9 : 
hex = '9';
break;
case 10 : 
hex = 'A';
break;
case 11 : 
hex = 'B';
break;
case 12 : 
hex = 'C';
break;
case 13 : 
hex = 'D';
break;
case 14 : 
hex = 'E';
break;
case 15 : 
hex = 'F';
break;
}
strcat(hex, finalHex);
finalHex = hex;
}
 printf("%s", finalHex);

回答by vishal_vvr

// decimal to hex converter
char* dec2hex(int num){
  int rem = 0,i=0;
  char hex[3];
  while(num > 0 && i >=0){
    rem = num%16;
    hex[i] = rem<10 ? (char)rem+48 : (char)rem+55;
    num/=16;
    i++;
  }
  hex[i]='##代码##';
  return strrev(hex);
}