Thread: hex string to integer...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    Question hex string to integer...

    Hi,

    Is there any library function available which will convert a passed hex string to its equivalent integer value???

    Something like atoi() except that the input contains a hexadecimal string ("1A2" => 418).

    TIA.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    Originally posted by Salem
    sscanf
    I'm not sure how. Consider this code:

    Code:
    int main()                    
    {                             
       char  str[50];             
       char  *hex_str   = "1A2";  
       int   int_output = 0;      
       
       /* how to get the hex value of hex_str in int_output such that
        * int_output has the integer value 
        *
        * using sscanf requires hex_str to be a hex integer pointer 
        * sscanf( str, "%x", hex_str ); 
        */
       
       /* should print "int_output is: 418" */
       printf( "int_output is: %d\n", int_output );
        
        
    } /* end of main() */
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    sscanf( hex_str, "%x", & int_output);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Or you strtol() can be used, as:
    Code:
    l_ret = strtol(o_str, (char **)NULL, 16);
    /*   Where, 16 tells the base of the number system (obviously 16 for HEX)   */
    Last edited by shaik786; 10-16-2002 at 07:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. hex string to integer...
    By xion in forum C Programming
    Replies: 4
    Last Post: 07-31-2003, 01:58 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM