# Decimal to Hex Conversion

• 08-09-2010
millsy2000
Decimal to Hex Conversion
Hey Guys,

Just a quick querry regarding some code i wrote to convert a decimal to hexadecimal, the code runs perfectly fine except for one hickup, it prints it backwards which is what I would expect. I was just wondering if there was a way I could flip the output. Any help would be greatly appreciated.

Code:

```printf("Enter a decimal :");  scanf("%d", &x);  printf("\n"); do{  if(x%16 < 10)     {     printf("%d", x % 16); x = x>>4;     }     else if(x%16 >=10)     {         if(x % 16 == 10)         printf("A");         else if(x % 16 == 11)         printf("B");         else if(x % 16 == 12)         printf("C");         else if(x % 16 == 13)         printf("D");         else if(x % 16 == 14)         printf("E");         else if(x % 16 == 15)         printf("F"); x=x>>4;     }  if(x<16) printf("%d", x); }while(x>1);```

Regards Sean
• 08-09-2010
qwertylurker
You could store the hex digits in a char array (or better, a linked list) and print out the array in reverse order near the end of the program.
• 08-09-2010
itCbitC
Recursion would be ideal for something like this.
• 08-09-2010
Dino
It would be simpler if you brought in the number as a string, and then just worked a digit at a time, left to right.
• 08-10-2010
iMalc
Quote:

Originally Posted by Dino
It would be simpler if you brought in the number as a string, and then just worked a digit at a time, left to right.

Yeah, except for that fact it couldn't give anything remotely close to the correct result that way! Perhaps you'd like to rethink that one.

millsy2000: Writing such a routine is trivial if you simply use a lookup table of "0123456789ABCDEF". Recursion would probably give the shortest solution here.
• 08-10-2010
Dino
Quote:

Originally Posted by iMalc
Yeah, except for that fact it couldn't give anything remotely close to the correct result that way! Perhaps you'd like to rethink that one.

Yes, rethinking that, I see the assignment is decimal to hex conversion, not character to hex conversion. My bad.
• 08-10-2010
pheininger
Just to make sure that we have covered the simple case, here is an UNTESTED example using one of printf()'s hexadecimal conversion specifiers.

Code:

```#include <stdio.h> int main(void) {   int x;   printf("Enter a decimal :");   scanf("%d", &x);   printf("\n");   printf("x is %X in hexadecimal.\n", x);   return 0; }```
You may be trying to understand how you would implement the converstion from int to hexadecimal. In that case, you either would have to build the result in a string. Either you would have to 1) start from the least significant digit at the end of the string buffer and work backwards through the string or 2) start from the beginning of the string with the least significant digit and print the string buffer in reverse. There are probably some other approachs that work.
• 08-10-2010
itCbitC
Here's my attempt at recursion:
Code:

```void d2x(int n) {     if (n/16)         d2x(n/16);     putchar(n%16 < 10 ? n%16 + '0' : (n%16)%10 + 'A'); }```