#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    199
    Rep Power
    12

    Exclamation Convert hex to octal(the display)


    hi there i have a function that converts(its just the display) binary, octal and hexadecimal into a decimal value
    Code:
    int convert2dec(int number, int base) {
         int total = 0;
         int power = 1;
        
         while(number > 0) { //keeps on doing this as long as the number is more than 0            
           
              total += number % 10 * power;
              number = number / 10;
              power = power * base;
         }
     
         return total;
    }
    what i want to ask if how would i go abt converting a hex to a decimal using this? lets say for example when the user enters 1A or 3F?

    e.g hex 1B = dec 27

    Thanks!! :D
    Last edited by icepricessa; April 25th, 2007 at 10:10 AM.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1803
    How does it do that!? An integer is an integer is an integer.

    Internally it is always binary, and externally is however you wish to represent it. You cannot hold a hexadecimal representation as anything other than a string, and your convert2dec() function does not take a string, it takes an integer. It also returns an int, which is not decimal either, it is only decimal if you choose to display a string representation of it in that way.

    Clifford
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    199
    Rep Power
    12
    Originally Posted by clifford
    How does it do that!? An integer is an integer is an integer.

    Internally it is always binary, and externally is however you wish to represent it. You cannot hold a hexadecimal representation as anything other than a string, and your convert2dec() function does not take a string, it takes an integer. It also returns an int, which is not decimal either, it is only decimal if you choose to display a string representation of it in that way.

    Clifford
    all its does is take an int value and manipulates its display? it doest really change the value of the int that is displayed. if a number entered was 11011 convert2dec(11011, 2) just will get the decimal conversion of it? kinda like in here
    http://www.dreamincode.net/code/snippet585.htm
    Last edited by icepricessa; April 25th, 2007 at 10:10 AM.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,322
    Rep Power
    2224
    Yuck!

    He has you thinking that you're entering a binary number, whereas it's actually reading it as a decimal. And then it goes through and changes that decimal value to a different decimal value? [shudder]

    What happens when you try to enter a binary number of practical size? Like something the size of 32 bits? I think that program limits you to only 10 bits, because just one more bit would cause the input integer (a 32-bit integer nowadays) to overflow.

    Why not just enter a string and convert that by parsing through each digit? That would also solve your issue of how to input hex numbers.

    In short, I think that the approach you're taking is hosed and you need to try a different approach.

    PS
    Shift-and-add. Shifting means multiplying by the base.
    Iterate through each character going from left to right.
    Preset your result value to zero.
    For each character, convert it to a numeric value. You would also test it at this point for being a valid digit, generating the appropriate error response. You would also test here for a termination character (ie, end of the string) whereupon you would exit.
    Shift the result value (ie, multiply by the base).
    Add the digit's value to the result.
    Continue the iteration.

    You should be able to handle any size of number, provided that its decimal value doesn't cause an overflow -- a 31-bit binary would be no problem, but for a 32-bit binary I think you would want to declare the result variable to be unsigned.
    Last edited by dwise1_aol; April 25th, 2007 at 10:49 AM.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    199
    Rep Power
    12
    Originally Posted by dwise1_aol
    Yuck!

    He has you thinking that you're entering a binary number, whereas it's actually reading it as a decimal. And then it goes through and changes that decimal value to a different decimal value? [shudder]

    What happens when you try to enter a binary number of practical size? Like something the size of 32 bits? I think that program limits you to only 10 bits, because just one more bit would cause the input integer (a 32-bit integer nowadays) to overflow.

    Why not just enter a string and convert that by parsing through each digit? That would also solve your issue of how to input hex numbers.

    In short, I think that the approach you're taking is hosed and you need to try a different approach.
    hmm... the things is this program is supposed to get the value(int) input like b11011 is binary 11011 and convert2dec(11011, 2), o27 is octal 27 etc... it just converts it to the decimal value and prints it out

    the only problem is when the input is for example h1B, how would i go abt assigning that B is 11?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,322
    Rep Power
    2224
    Read my PS.

    In that program you have, try entering a 12-bit binary and see what happens.

    That program reads in a decimal value. It should read in a string and then that string should be converted.

    If you read in a string, then you can handle hex. If you read in a decimal value, then you cannot handle hex, because scanf's conversion of the input string to a decimal will fail.


    PS
    I compiled and ran that program, which I named HOSED.C:
    C:> hosed
    Enter an interger (0's and 1's): 111111111111
    The decimal equilavent is 0.
    'Nuff said?
    Last edited by dwise1_aol; April 25th, 2007 at 11:04 AM.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    199
    Rep Power
    12
    hi thanks for your explanation maybe this would be clearer?
    Last edited by icepricessa; April 26th, 2007 at 07:51 AM.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,322
    Rep Power
    2224
    Converts a string in any base to decimal:
    Code:
    unsigned int basetodec(char* value, int base)
    {
        int total = 0;
        int digit;
        char *cp = value;
        char ch;
         
        // set up an infinite loop.
        // use break statement to exit it. 
        for ( ; ; cp++)
        {
            ch = toupper(*cp);
            if (ch >= '0' && ch <= '9')
                digit = ch - '0';
            else if (ch >= 'A' && ch <= (base - 11 + 'A'))
                digit = ch - 'A' + 10;
            else 
                break;        
                
            total *= base;
            total += digit;     
        }
    
        return total;
    }

    You could simplify your main by coverting the base letter to its equivalent decimal value.

    Also, I don't see how you would handle hex digits a-f.
    Last edited by dwise1_aol; April 25th, 2007 at 11:43 AM.
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1803
    Originally Posted by icepricessa
    if a number entered was 11011 convert2dec(11011, 2) just will get the decimal conversion of it?
    Yes, as I said it does not work. Show me an example of this function call for a number base greater than 10 - see it is fundamentally flawed as an idea!

    Clifford
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    134
    Converting the text representation of an integer in bases 2-36: strtol and friends.
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

IMN logo majestic logo threadwatch logo seochat tools logo