Unix, C, and C++
Function Reference
Numeric Conversions




The first three functions are available only in C++, the remainder are also available in C.



  int stoi(string s, size_t * ptr, int base)
      include: <string>
      Converts a string representing a decimal integer into an int.
      It takes characters from the string s, so long as they make a valid int. As soon
      as a characted that can't be part of an int is reached, the conversion is over.
      The variable that ptr points to (size_t is just a large form of int) is set to
      the index in the string of that unacceptable character. Usually that means that
      if the size_t is not equal to s.length() - 1, the string was invalid. If ptr is
      NULL, it is just ignored. The default value for base is 10, if base is set to 0,
      it will look for any leading 0x etc or their absence to determine the base.



  long long int stoll(string s, size_t * ptr, int base)
      include: <string>
      This is exactly the same as stoi except that it produces a
      64 bit result.



  double stod(string s, size_t * ptr)
      include: <string>
      Converts a string representing a floating point number into an double.
      It takes characters from the string s, so long as they make a valid double. As
      soon as a characted that can't be part of an double is reached, the conversion
      is over. The variable that ptr points to (size_t is just a large form of int)
      is set to the index in the string of that unacceptable character. Usually that
      means that if the size_t is not equal to s.length() - 1, the string was invalid
      If ptr is NULL, it is just ignored. The default value for base is 10, if base
      is set to 0, it will look for any leading 0x etc or their absence to determine
      the base.



  int atoi(char * s)
      include: <stdlib.h>
      Converts a string representing a decimal integer into an int.
      The initial portion of the string consisting only of decimal digits is converted
      into the int that it represents. As soon as a character that can not be part of
      a decimal integer is encountered, the conversion is finished. No errors are
      detected or reported.
      refer also to strtol, which is generally better and detects
      errors.



  long long int atoll(char * s)
      include: <stdlib.h>
      Do not use this function!
      atoi is the same as atoi, except that it produces 64 bit ints.



  double atof(char * s)
      include: <stdlib.h>
      Converts a string representing a floating point number into a double.
      The initial portion of the string consisting of a valid number is converted
        into the int that it represents. As soon as a character that can not be part of
        a decimal floating-point number is encountered, the conversion is finished. 
      No errors are detected or reported..
      Refer also to strtod, which is generally better and detects
      errors.



  int strtol(char * s, char * * end, int base)
      include: <stdlib.h>
      Converts a string representing an integer into an int.
      The second parameter must be the address of a char * variable.
      The third parameter is the base to be used in the conversion, must be between
         2 and 36 (or zero which means "work it out for yourself").
      Characters are taken from the beginning of the first (string) parameter so long
        as they form a valid integer in the given base. As soon as an invalid character
        is reached, the conversion finishes and a pointer to the first invalid character
        is stored in the variable whose address was provided in the second parameter.
        If the entire string made a valid integer, this variable will point to the '\0'.
      If the second parameter is NULL, it is ignored.
      If the base is given as 0, it is determined by context. A string beginning with
        "0x" is taken as base 16, "0" means base 8, and anything else means base 10.
      Example:
        char * s="1234";
        char * end;
        int n = strtol(s, &end, 10);
        if (*end == '\0')
          printf("%s = %d\n", s, n);
        else
          printf("%s is not a valid decimal integer\n", s);
          


  long long int strtoll(char * s, char * * end, int base)
      include: <stdlib.h>
      The same as strtol but it produces 64 bit ints.



  double strtod(char * s, char * * end)
      include: <stdlib.h>
      Converts a string representing a floating point number into a double.
      The second parameter must be the address of a char * variable.
      Characters are taken from the beginning of the first (string) parameter so long
        as they form a valid number. As soon as an invalid character is reached, the 
        conversion finishes and a pointer to the first invalid character is stored in 
        the variable whose address was provided in the second parameter.
        If the entire string made a valid number, this variable will point to the '\0'.
      If the second parameter is NULL, it is ignored.
      Example:
        char * s="+12.34e-02";
        char * end;
        double x = strtod(s, &end, 10);
        if (*end == '\0')
          printf("%s = %f\n", s, x);
        else
          printf("%s is not a valid number\n", s);


See also:
  sscanf: general conversion from string to anything
  sprintf: general conversion from anything to string