How to make a decimal to hexadecimal converter in

2019-08-21 05:08发布

问题:

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

So I guess I would need to make a loop that loops until the result is 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.

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.

回答1:

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.

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

(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.)



回答2:

I'd simply use sprintf, to be honest:

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);


回答3:

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].



回答4:

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);


回答5:

// 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]='\0';
  return strrev(hex);
}


标签: c hex decimal