Thread: Conversion Question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    44

    Conversion Question

    How can I convert an in int stored in hex such as
    int x = (0x2710); to decimal 2710?

    For example, I want to set another variable
    x= y, but y would be decimal and equal to 2710.

    I have searched the web and this forum and was not able to find guidance ( I only saw examples of conversion where you actually read the hex value as user input and convert one character at a time, where as I am just simply trying to convert a known hex value), so I would appreciate any help.
    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2016
    Posts
    9
    Just for clarification, you're trying to directly take a hex number, and convert it to a decimal number that shares the same digits?

    Like, in your example it sounds like you want to take

    2710hex and convert it to 2710dec?

    Or, 4618h becomes 4618d?

    You can write a routine that does this, but how would you like to handle the situation where you have a hex number that doesn't have digits 0-9, like for example the number 0x24E7?

    Matt

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Both scanf() and printf() have format specifiers that can retrieve or print either unsigned decimal or unsigned hex numbers.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        unsigned int num = 0x2710;
        printf("%u %#x\n", num, num);
        return 0;
    }
    Edit: Remember computers store the numbers in binary and convert them to/from decimal or hex for the sake of the users/programmers.


    Jim
    Last edited by jimblumberg; 08-18-2016 at 10:51 AM.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    Quote Originally Posted by meiermat View Post
    Just for clarification, you're trying to directly take a hex number, and convert it to a decimal number that shares the same digits?

    Like, in your example it sounds like you want to take

    2710hex and convert it to 2710dec?

    Or, 4618h becomes 4618d?


    You can write a routine that does this, but how would you like to handle the situation where you have a hex number that doesn't have digits 0-9, like for example the number 0x24E7?

    Matt
    Yes, that is correct. The number will always have digits only/ never characters. Any suggestions on the implementation of the routine?

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    Quote Originally Posted by jimblumberg View Post
    Both scanf() and printf() have format specifiers that can retrieve or print either unsigned decimal or unsigned hex numbers.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        unsigned int num = 0x2710;
        printf("%u %#x\n", num, num);
        return 0;
    }
    Edit: Remember computers store the numbers in binary and convert them to/from decimal or hex for the sake of the users/programmers.


    Jim

    Jim, I am not looking just to print it.
    Once I store the value as a decimal into another variable, I need to be able to do comparisons of the new decimal value against other decimals.

  6. #6
    Registered User
    Join Date
    Aug 2016
    Posts
    9
    I assume your original value is stored as an integer of some type? If so, you basically just have to convert it to a hex number, like as if you were doing it all by hand. For example, suppose you have the number 0x2710 stored in the computer. The computer doesn't know it's in hex. It's just a number, equal to 10,000d or 0010 0111 0001 0000b.

    For now, I'll assume you know the number is four digits in hex. This process will determine the first digit, then the next, etc. by successively dividing by the appropriate powers of sixteen, and then multiplying by the equivalent power of ten.

    Code:
      int temp;
      int decVal = 0;
      int hexVal = 0x2710;
    
      temp = hexVal / 4096;  //In C, this should always round down and extract the first digit.
      decVal += temp * 1000;  //This converts the first digit to 2 in base-10
      hexVal = hexVal % 4096;  //This leaves the remaining value, after removing the first digit from the original value
    
      temp = hexVal / 256;
      decVal += temp * 100;
      hexVal = hexVal % 256;
    
      //keep going until you've gotten down to the last digit
    Personally, I would set this up in a "while" loop with an index that increments to the powers of 16.

    This isn't necessarily the most efficient way; if you have a 32-bit number, for example, but your value is only 0x00000014, then you waste a lot of effort to add a whole bunch of zeroes. You could set up a preliminary sequence of if-then statements to check the size, or a loop to determine the magnitude of the number in base-16.

    On the other hand, if you know you always have a 32-bit number that's greater than 2^30, you would waste a lot of time with the initial magnitude check. At any rate, it's basically just an optimization problem now, and that's at least partially dependent on whether you know how big your numbers will be in advance.

    Matt
    Last edited by meiermat; 08-18-2016 at 01:15 PM.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Alternatively:
    Code:
        int x = 0x2710, y;
        char s[50];
        
        // Create a character representation of the hex value.
        sprintf(s, "%x", x);
    
        // Read the character rep. as a decimal value.
        sscanf(s, "%d", &y);
    
        printf("%d\n", y);   // should print 2710
    This is a strange thing to do.
    May I ask why you want to do this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type conversion question.
    By ellis in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2006, 04:05 PM
  2. Conversion Type Question ?
    By cisokay in forum C Programming
    Replies: 1
    Last Post: 05-10-2005, 07:47 PM
  3. Quick Conversion Question
    By DougC in forum C Programming
    Replies: 3
    Last Post: 01-10-2003, 07:12 PM
  4. conversion question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 07:24 PM
  5. hexadecimal conversion question
    By KnightKap in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 11:49 PM

Tags for this Thread