How to extract a hex value from a string?

This is a discussion on How to extract a hex value from a string? within the C++ Programming forums, part of the General Programming Boards category; I need to exact a hex value from within a string and manupulate it the numeric value within a program. ...

  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
    DLaw@barton-canada.com

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

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