string to integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • quickcur@yahoo.com

    #1

    string to integer

    If I have a string in Hex, e.g., "0xff", how can I turn it into
    numbers?

    Thanks,

    qq

  • Victor Bazarov

    #2
    Re: string to integer

    quickcur@yahoo. com wrote:[color=blue]
    > If I have a string in Hex, e.g., "0xff", how can I turn it into
    > numbers?[/color]

    Discard the '0x' and use strtod to convert the rest.

    Comment

    • Alan

      #3
      Re: string to integer


      <quickcur@yahoo .com> wrote in message news:1103742581 .120693.155210@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > If I have a string in Hex, e.g., "0xff", how can I turn it into
      > numbers?[/color]

      long strtol( const char *s, char **endptr. int radix) in stdlib.h

      If radix is zero and first char is "0" and second is "x" (or "X"), string is
      interpreted as hexadecimal.

      long num = strtol("0xff", NULL, 0);



      Comment

      • Robert Swan

        #4
        Re: string to integer

        quickcur@yahoo. com wrote:[color=blue]
        > If I have a string in Hex, e.g., "0xff", how can I turn it into
        > numbers?[/color]

        if none of the ios_base::hex/dec/oct are set on a std input stream then
        the prefix determines the base for string formatting

        #include <iostream>
        #include <string>
        #include <sstream>

        int main() {
        std::string n_string("0xff" );
        std::istringstr eam n_stream(n_stri ng);
        n_stream.unsetf (std::ios_base: :dec);

        int result;
        n_stream >> result;
        std::cout << result << std::endl; // should output 255
        }

        Comment

        Working...