Hex String to Decimal Possible?

This is a discussion on Hex String to Decimal Possible? within the C Programming forums, part of the General Programming Boards category; Hello, I have a program that takes input from a user with gets(). Basically, the user is going to input ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    Hex String to Decimal Possible?

    Hello, I have a program that takes input from a user with gets(). Basically, the user is going to input a hexidecimal number. I will need to convert that number to an int because its going to have to be used in array indices etc. Is there a C library that can do this? Anyone know where I can locate one if not ANSI? Thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by someprogr View Post
    Hello, I have a program that takes input from a user with gets(). Basically, the user is going to input a hexidecimal number. I will need to convert that number to an int because its going to have to be used in array indices etc. Is there a C library that can do this? Anyone know where I can locate one if not ANSI? Thanks.
    1. do not use gets - read FAQ
    2. read about strtol, sscanf, format %i and %x
    http://msdn.microsoft.com/en-us/library/6ttkkkhh.aspx
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    Try this: (http://www.gidnetwork.com/b-44.html)
    Code:
    // This program will convert a string value into it's internal
    // binary equivalent for any base up to 36. The conversion will
    // stop at the first invalid character for the base specified
    // (which includes the \\n or \\0)
    
    #include <stdio.h>
    int Convert(char *, int);
    
    int main()
    {
        char inbuf[32];
        int  base;      // Base for the conversion
        int  binary;    // final binary value
        int  j;         // loop counter
    
        printf("Enter the base (in decimal): ");
        fgets(inbuf, 32, stdin);    // Get the base for conversion
        sscanf(inbuf,"%d",&base);   // Make it a value
    
        printf("Enter the base %d value to convert: ", base);
        fgets(inbuf, 32, stdin);    // Get the value for conversion
    
        binary = Convert(inbuf, base);
        printf("%d  %Xh \n", binary, binary);  // output dec & hex
        return 0;
    }
    
    int Convert(char *buf, int cbase)
    {
        int j;
        int bin;
    
        j = 0;
        bin = 0;                 // start the binary value at 0
        while (buf[j])
        {
            buf[j] -= 48;           // convert character for binary
            if (buf[j] > 16)
                buf[j] -= 7;        // character was probably letter
            if (buf[j] >= cbase)
                buf[j] -= 32;       // character was probably lower
            if (buf[j] >= cbase || buf[j] < 0)
                break;              // invalid character, done
            bin *= cbase;           // multiply by the base
            bin += buf[j];          // add current value
            j++;                    // next character
        }
        return bin;             // return the binary value
    }
    Last edited by auralius; 02-01-2009 at 12:26 AM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you shoudl not use magic numbers

    and what is wrong with the standard functions?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90

    so that the brain works...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Signed hex string to a int
    By naruto267 in forum C Programming
    Replies: 5
    Last Post: 05-18-2008, 11:43 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 10:37 AM
  5. about decimal to hex progam
    By Abdi in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 07:08 PM

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