CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 1999
    Posts
    5

    Convertion of hex string to integer...



    Hi all,


    Does anyone know how can I convert hexadecimal string to int or long type

    using MFC or other libs.

    For example:

    char *hexstr = "101E4";

    int i = ?HexToIntFunc?(hexstr);


    Thanks in advance


    Serge



  2. #2
    Join Date
    Apr 1999
    Posts
    191

    I use strtol()



    It's not an mfc function, but I don't know of any mfc functions that do it.

  3. #3
    Guest

    Re: Convertion of hex string to integer...

    You can convert hex number to int by using sscanf and %X - reading hex like this.

    char *inp = "A1D2" ;
    int i;
    sscanf( inp, "%X", &i ) ;

    It's as simple as that.

    Best Wishes,
    Sriram.


  4. #4
    Guest

    strtol won't work.

    strtol is for long. It can't accept hex strings.


  5. #5
    Join Date
    May 1999
    Location
    Seattle, WA USA
    Posts
    423

    Re: strtol won't work.

    why can't strtol except a hex number? a hex number is still a long, Hex is not a data type, it is a certain base of a number.
    strtol looks like

    long strtol( const char *nptr, char **endptr, int base );




    just set the base to 16.

    --michael




  6. #6
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: strtol won't work.

    One really handy feature is that if you pass a base of 0 it will interpret the string
    based on the first characters it finds. If the string starts with '0x' it will use a hex
    interpretation. If it starts with a zero and not an X then it uses octal.
    This convention allows you to mix types and strtol will sort things out for you.




  7. #7
    Join Date
    May 1999
    Location
    Greenville, SC, USA
    Posts
    33

    Re: Convertion of hex string to integer...

    Of course, Its more fun to write your own function to convert the string into hex.
    ~G²


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured