I am studying on my own the different functions that the ANSI-C standard libraries offer us. I end up getting stuck with the strtol function.
long int strol(const char *numPtr, char **finalPtr, int base);
The theory so far found goes like this:
strtol: Converts the initial portion of the string pointed to by numPtr to a long int representation .
It first decomposes the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by the isspace function), a source sequence resembling an integer represented in some base determined by the value of base , and a final string of one or more unrecognized characters, including the final null character of the input string. So, it tries to convert the source sequence to an integer, and returns the result.
If the base value is zero, the expected form of the source sequence is that of an integer constant, optionally preceded by a plus or minus sign, but not including an integer suffix. If the base value is between 2 and 36, the expected form of the source sequence is a sequence of letters and digits representing an integer with a base specified by base, optionally preceded by a positive or negative sign, but not including an integer suffix. .
The letters a (or A) through z (or Z) are assigned the values from 10 to 35; only letters whose attributed values are less than those of the base are allowed. If the base value is 16, the characters 0x or 0X can optionally precede the sequence of letters and digits, following the > sign, if it is present.
The source sequence is defined as the longest initial sequence of the input string, starting with the first non-white space character, which is as expected. The source sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white space character is other than an allowed sign or letter or digit.
If the source sequence has the expected form and the base value is zero, the sequence of characters beginning with the first digit is interpreted as an integer constant. If the source sequence has the expected form and the base value is between 2 and 36, it is used as the base for the conversion, giving each letter its given value as described above. If the source sequence begins with a negative sign, the resulting value of the conversion is negative. A pointer to the final string is stored in the object pointed to by finalPtr, as long as finalPtr is not null. If the source stream is empty or does not have the expected form, no conversion is performed; the value numPtr is stored in the object pointed to by finalPtr, as long as finalPtr is not null.
The format used by this function is shown below:
[eb] [sn] [0] [x] [ddd] ,where:[eb] Optional whitespace Blockquote
[sn] Optional sign (+ or -)
[0] Optional zero (0)
[x] 'x' or 'X' optional
[ddd] Optional digits
If base is zero, the first characters of numPtr determine the base:
First character -----Second character ----- String interpreted as...
0 ------------------------- 1 to 7 --------------------- ---- Octal
0 ------------------------- x or X ---------------- --------- Hexadecimal
1 to 9 ---------------------(0 to 9) ----------- ------------- DecimalThe strtol function returns the converted value, if it exists. If no conversion could be performed, zero is returned. If the correct value does not belong to the range of representable values, LONG_MAX or LONG_MIN is returned (depending on the sign of the value), and the value of the ERANGE macro is stored in errno.
What I don't understand is for what purpose this function is used.
For example:
#include <stdio.h>
#include <stdlib.h>
int main(){
char numPtr[9] = "12345678", finalPtr;
int base;
for( base=0; base<=20; base++ )
printf( "Convirtiendo la cadena \"%s\" en un numero en base %d: %u\n", numPtr,base, strtol(numPtr,finalPtr, base) );
return 0;
}
This example gives us the following outputs:
I do not understand the reason for the exits of this program. I understand that strtol saves a string as a long int and that in base 0 and 10 it returns the original value of the string.
I don't understand the definition of "in base" because if we talk about binary, octal, decimal, and hexadecimal... I don't understand why the output in base 2 gives 1 and not the string transformed into binary, and the same with the other numbers .
Well actually this function has me confused.
Could someone explain to me what is the process that the function follows to get these results? Or show me a more intuitive example for the good use of this function?
PS Thanks in advance for the inconvenience of reading such a ramble ^^!
The example is taken from the ANSI-C manual at http://www.conclase.net/