C++ cstdlib strtol , strtoll , strtoul , and strtoull


The C++ <cstdlib> strtol,strtoll,strtoul and strtoull convert the string to integer value.The declaration of the functions are given below.

long int strtol(const char * restrict x , char ** restrict endx , int base);
long long int strtoll(const char * restrict x , char ** restrict endx , int base);
unsigned long int strtoul(const char * restrict x , char ** restrict endx , int base);
unsigned long long int strtoull(const char * restrict x , char ** restrict endx , int base);

Parameters:
x -The string to be converted to integer value

endx -A pointer which stores some string(explained later).

base -The base value which is use for converting the string to integer value.

Return type
integer value – long int , long long , unsigned long int and unsigned long long type is returned.

Some points to note:

i) At first the string is decompose into three parts:

  a)An empty sequence containing white-space.

  b)A sequence resembling the integer sequence which is to be converted into the integer.

  c)The final string consist of one or more unrecognized characters, including the terminating null character.

After this the second part is converted into the appropriate integer value.


ii) If base value(the third argument) is 0
  a)The string is converted to the integer type.

  b)If the string has the initial characters as ‘0X’ or ‘0x’ the string is treated as hexadecimal digits and the conversion method is based on the rule of hexadecimal type to integer type conversion method.

Code example

char *c ;

cout<< strtol( “23459” , &c , 0 ) << endl
<< strtol( “-0xab4242” , &c , 0 ) ; ///the sign will be preserved

Output in code::block

23459
-11223618


iii) If base value is between 2 to 36:
  a)If the third argument or the base value is any between 2 to 36 the conversion of the string is performed treating the string as the digits of the ‘base’ type.For instance if the third argument or the base value is 8 the string characters is treated as the octal digits and the conversion is perform as though we are converting octal to decimal value.In such case the value 9 is excluded.

  b)For the base value 10 to 35 the character a(or A) to z(or Z) will represent the value 10 to 35.

Note in short the base value specify how the string is converted to the integer value.The concept is same as binary(base 2) , octal(base 8) or hexadecimal(base 16) format,where in each format the digits used to represented them is less than the base value.Another example is,say if the third argument passed is 20(the base value is 20) the letter after ‘j’ will not be used to convert to the integer value.

Code example

char *c ;

cout<< strtol( “23459” , &c , 5 ) << endl
<< strtoll( “637abcgkl” , &c , 12 ) << endl ;

cout<< strtol( “-ah7mw45” , &c , 30 ) ;

Output in CB,

69 (convert 234 to digit using only up to the number 4(5 excluded) format)
130739
-285532

For the second output ‘130739’ let us see how we got this output.The base 12 will correspond to the character ‘c’ so only the string ‘637ab’ will be used to convert to integer value.How to convert the string to the value is shown below.

Like the binary or any other format while converting the string,it will be given the index value from 0 and will increment as we go leftward as shown below,
C++ strtoll function
Using the index and the value of the character (a=10 , b=11),we will apply the formula,

6*124 + 3*123 + 7*122 + 10*121 + 11*120

The result is 130739 which is the output.

For the third output use 30 instead of 12 and apply the same process you will get the sum as the output.





iv)If base value is 16 the string will be treated as hexadecimal digits and the conversion operation is done as though we are converting hexadecimal digits to decimal values.In this case whether the string has the initial characters as ‘0x’ or not does not matter,the string is treated as hexadecimal digits by default.

Code example

char *c ;

cout<< strtoul( “-a” , &c , 16 ) << endl
<< strtoll( “6327abdko” , &c , 16 ) ; //the characters after ‘d’ is neglected

Output in CB,

-10
103971517


v)If empty string is passed or the string is non-convertible the functions returned 0.And the string is stored in the third argument.

Code example

char *c ;

cout<< strtoul( “l827ab” , &c , 20 ) << endl
<< strtoul( “zb647” , &c , 35 ) << endl
<< strtoul( “” , &c , 20 ) << endl ;

cout<< *c ;

Output in CB,

0
0
0
0

The *c value is 0.


vi) If the value converted is not representable by the return type the value returned is LONG_MIN , LONG_MAX,LLONG_MIN,LLONG_MAX,ULONG_MAX , ULLONG_MAX and the value of the macro ERANGE is stored in errno.Include the header <cmath> to check the value of the macros.

Code example

char *c ;

cout<< strtol( “982138238923898293899238” , &c , 20 ) << endl ;

cout<< LONG_MIN << endl
<< LONG_MAX << ;

Output in CB

2147483647
-2147483648
2147483647

You can check the value of the remaining macros yourself.


Lastly: Since ‘NAN’ and ‘INFINITY’ belong to floating point type you cannot obtain them using these functions.

Link C++ cmath nan

You cannot pass a string variable or a literal of string type as the first argument.You can only pass a ‘char*’ variable or a string literal.

Code example

char *c ;
string s=”235″ ;
char cc[]=”738″ ;

cout<< strtoul( “NAN” , &c , 20 ) << endl ;

/* cout<< strtol( s , &c , 20 ) ; ///error
cout<< strtoul( string(“838”) , &c , 9 ); ///error */

cout<< strtoul( cc , &c , 8 ) ; //work fine

Output in CB

73