In Section 20.8, we discussed several of C++’s most popular C string-manipulation functions. In the next several sections, we cover the remaining functions, including functions for converting strings to numeric values, functions for searching strings and functions for manipulating, comparing and searching blocks of memory.
This section presents the C string-conversion functions from the general-utilities library <cstdlib>. These functions convert C strings to integer and floating-point values. In new code, C++ programmers typically use the string stream processing capabilities (Chapter 19) to perform such conversions. Figure 20.27 summarizes the C string-conversion functions. When using functions from the general-utilities library, include the <cstdlib>
header.
Function atof (Fig. 20.28, line 9) converts its argument—a string that represents a floating-point number—to a double
value. The function returns the double
value. If the string cannot be converted—for example, if the first character of the string is not a digit—function atof
returns zero.
1 // Fig. 20.28: fig20_28.cpp
2 // Using atof.
3 #include <iostream>
4 #include <cstdlib> // atof prototype
5 using namespace std;
6
7 int main()
8 {
9 double d = atof( "99.0" ); // convert string to double
10
11 cout << "The string "99.0" converted to double is " << d
12 << "
The converted value divided by 2 is " << d / 2.0 << endl;
13 } // end main
The string "99.0" converted to double is 99
The converted value divided by 2 is 49.5
Function atoi (Fig. 20.29, line 9) converts its argument—a string of digits that represents an integer—to an int
value. The function returns the int
value. If the string cannot be converted, function atoi
returns zero.
1 // Fig. 20.29: fig20_29.cpp
2 // Using atoi.
3 #include <iostream>
4 #include <cstdlib> // atoi prototype
5 using namespace std;
6
7 int main()
8 {
9 int i = atoi( "2593" ); // convert string to int
10
11 cout << "The string "2593" converted to int is " << i
12 << "
The converted value minus 593 is " << i - 593 << endl;
13 } // end main
The string "2593" converted to int is 2593
The converted value minus 593 is 2000
Function atol (Fig. 20.30, line 9) converts its argument—a string of digits representing a long integer—to a long
value. The function returns the long
value. If the string cannot be converted, function atol
returns zero. If int
and long
are both stored in four bytes, function atoi
and function atol
work identically.
1 // Fig. 20.30: fig20_30.cpp
2 // Using atol.
3 #include <iostream>
4 #include <cstdlib> // atol prototype
5 using namespace std;
6
7 int main()
8 {
9 long x = atol( "1000000" ); // convert string to long
10
11 cout << "The string "1000000" converted to long is " << x
12 << "
The converted value divided by 2 is " << x / 2 << endl;
13 } // end main
The string "1000000" converted to long int is 1000000
The converted value divided by 2 is 500000
Function strtod (Fig. 20.31) converts a sequence of characters representing a floating-point value to double
. Function strtod
receives two arguments—a string (char *
) and the address of a char *
pointer (i.e., a char **
). The string contains the character sequence to be converted to double
. The second argument enables strtod
to modify a char *
pointer in the calling function, such that the pointer points to the location of the first character after the converted portion of the string. Line 12 indicates that d
is assigned the double
value converted from string
and that stringPtr
is assigned the location of the first character after the converted value (51.2
) in string
.
1 // Fig. 20.31: fig20_31.cpp
2 // Using strtod.
3 #include <iostream>
4 #include <cstdlib> // strtod prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "51.2% are admitted";
10 char *stringPtr = nullptr;
11
12 double d = strtod( string1, &stringPtr ); // convert to double
13
14 cout << "The string "" << string1
15 << "" is converted to the
double value " << d
16 << " and the string "" << stringPtr << """ << endl;
17 } // end main
The string "51.2% are admitted" is converted to the
double value 51.2 and the string "% are admitted"
Function strtol (Fig. 20.32) converts to long
a sequence of characters representing an integer. The function receives a string (char *
), the address of a char *
pointer and an integer. The string contains the character sequence to convert. The second argument is assigned the location of the first character after the converted portion of the string. The integer specifies the base of the value being converted. Line 12 indicates that x
is assigned the long
value converted from string
and that remainderPtr
is assigned the location of the first character after the converted value (-1234567
) in string1
. Using a null pointer for the second argument causes the remainder of the string to be ignored. The third argument, 0
, indicates that the value to be converted can be in octal (base 8), decimal (base 10) or hexadecimal (base 16). This is determined by the initial characters in the string—0
indicates an octal number, 0x
indicates hexadecimal and a number from 1
to 9
indicates decimal.
1 // Fig. 20.32: fig20_32.cpp
2 // Using strtol.
3 #include <iostream>
4 #include <cstdlib> // strtol prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "-1234567abc";
10 char *remainderPtr = nullptr;
11
12 long x = strtol( string1, &remainderPtr, 0 ); // convert to long
13
14 cout << "The original string is "" << string1
15 << ""
The converted value is " << x
16 << "
The remainder of the original string is "" << remainderPtr
17 << ""
The converted value plus 567 is " << x + 567 << endl;
18 } // end main
The original string is "-1234567abc"
The converted value is -1234567
The remainder of the original string is "abc"
The converted value plus 567 is -1234000
In a call to function strtol
, the base can be specified as zero or as any value between 2 and 36. (See Appendix D for a detailed explanation of the octal, decimal, hexadecimal and binary number systems.) Numeric representations of integers from base 11 to base 36 use the characters A–Z to represent the values 10 to 35. For example, hexadecimal values can consist of the digits 0–9 and the characters A–F. A base-11 integer can consist of the digits 0–9 and the character A. A base-24 integer can consist of the digits 0–9 and the characters A–N. A base-36 integer can consist of the digits 0–9 and the characters A–Z. [Note: The case of the letter used is ignored.]
Function strtoul (Fig. 20.33) converts to unsigned long
a sequence of characters representing an unsigned long
integer. The function works identically to strtol
. Line 13 indicates that x
is assigned the unsigned long
value converted from string
and that remainderPtr
is assigned the location of the first character after the converted value (1234567
) in string1
. The third argument, 0
, indicates that the value to be converted can be in octal, decimal or hexadecimal format, depending on the initial characters.
1 // Fig. 20.33: fig20_33.cpp
2 // Using strtoul.
3 #include <iostream>
4 #include <cstdlib> // strtoul prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "1234567abc";
10 char *remainderPtr = nullptr;
11
12 // convert a sequence of characters to unsigned long
13 unsigned long x = strtoul( string1, &remainderPtr, 0 );
14
15 cout << "The original string is "" << string1
16 << ""
The converted value is " << x
17 << "
The remainder of the original string is "" << remainderPtr
18 << ""
The converted value minus 567 is " << x - 567 << endl;
19 } // end main
The original string is "1234567abc"
The converted value is 1234567
The remainder of the original string is "abc"
The converted value minus 567 is 1234000