Thread: How to extract a hex value from a string?

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

    How to extract a hex value from a string?

    I need to exact a hex value from within a string and manupulate it the numeric value within a program.

    For example:

    the string = "C60000F0"
    I need to get the numeric value of this, which in this case is 3321889008.

    atol nor any of the other standard functions seem to work. Is there some other way in which I can do this?

    - Dave
    [email protected]

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try strtoul().
    Code:
       char hex[9] = "C60000F0";
       unsigned long num;
       char *endptr;
    
       num = strtoul(hex,&endptr,16);
       cout << hex << endl;
       cout << num << endl;

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    3
    Thanks. That worked.

    - Dave

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. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Converting a binary string to hex
    By maxhavoc in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2006, 12:46 PM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM