Decimal to Hex Conversion

This is a discussion on Decimal to Hex Conversion within the C Programming forums, part of the General Programming Boards category; Hey Guys, Just a quick querry regarding some code i wrote to convert a decimal to hexadecimal, the code runs ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    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

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Recursion would be ideal for something like this.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Quote Originally Posted by Dino View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by iMalc View Post
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    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.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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');
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-04-2008, 12:39 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 01:14 AM
  5. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 10:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21