22.9 C String-Conversion Functions

In Section 22.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 21) to perform such conversions. Figure 22.27 summarizes the C string-conversion functions. When using functions from the general-utilities library, include the <cstdlib> header.

Fig. 22.27 C string-conversion functions of the general-utilities library.

Prototype Description
double atof(const char* nPtr) Converts the string nPtr to double. If the string cannot be converted, 0 is returned.
int atoi(const char* nPtr) Converts the string nPtr to int. If the string cannot be converted, 0 is returned.
long atol(const char* nPtr) Converts the string nPtr to long int. If the string cannot be converted, 0 is returned.
double strtod(const char* nPtr, char** endPtr)
  Converts the string nPtr to double. endPtr is the address of a pointer to the rest of the string after the double. If the string cannot be converted, 0 is returned.
long strtol(const char* nPtr, char** endPtr, int base)
  Converts the string nPtr to long. endPtr is the address of a pointer to the rest of the string after the long. If the string cannot be converted, 0 is returned. The base parameter indicates the base of the number to convert (e.g., 8 for octal, 10 for decimal or 16 for hexadecimal). The default is decimal.
unsigned long strtoul(const char* nPtr, char** endPtr, int base)
  Converts the string nPtr to unsigned long. endPtr is the address of a pointer to the rest of the string after the unsigned long. If the string cannot be converted, 0 is returned. The base parameter indicates the base of the number to convert (e.g., 8 for octal, 10 for decimal or 16 for hexadecimal). The default is decimal.

Function atof (Fig. 22.28, line 8) 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.

Fig. 22.28 String-conversion function atof.

Alternate View

 1  // Fig. 22.28: fig22_28.cpp
 2  // Using atof.
 3  #include <iostream>
 4  #include <cstdlib> // atof prototype
 5  using namespace std;
 6
 7  int main() {
 8     double d{atof("99.0")}; // convert string to double
 9
10     cout << "The string "99.0" converted to double is " << d
11        << "
The converted value divided by 2 is " << d / 2.0 << endl;
12  }

The string "99.0" converted to double is 99
The converted value divided by 2 is 49.5

Function atoi (Fig. 22.29, line 8) 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.

Fig. 22.29 String-conversion function atoi.

Alternate View

 1  // Fig. 22.29: fig22_29.cpp
 2  // Using atoi.
 3  #include <iostream>
 4  #include <cstdlib> // atoi prototype
 5  using namespace std;
 6
 7  int main() {
 8     int i{atoi("2593")}; // convert string to int
 9
10     cout << "The string "2593" converted to int is " << i
11        << "
The converted value minus 593 is " << i - 593 << endl;
12  }

The string "2593" converted to int is 2593
The converted value minus 593 is 2000

Function atol (Fig. 22.30, line 8) 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.

Fig. 22.30 String-conversion function atol.

Alternate View

 1  // Fig. 22.30: fig22_30.cpp
 2  // Using atol.
 3  #include <iostream>
 4  #include <cstdlib> // atol prototype
 5  using namespace std;
 6
 7  int main() {
 8     long x{atol("1000000")}; // convert string to long
 9
10     cout << "The string "1000000" converted to long is " << x
11        << "
The converted value divided by 2 is " << x / 2 << endl;
12  }

The string "1000000" converted to long int is 1000000
The converted value divided by 2 is 500000

Function strtod (Fig. 22.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 11 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.

Fig. 22.31 String-conversion function strtod.

Alternate View

 1  // Fig. 22.31: fig22_31.cpp
 2  // Using strtod.
 3  #include <iostream>
 4  #include <cstdlib> // strtod prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char*string1{"51.2% are admitted"};
 9     char* stringPtr{nullptr};
10
11     double d{strtod(string1, &stringPtr)}; // convert to double
12
13     cout << "The string "" << string1
14        << "" is converted to the
double value " << d
15        << " and the string "" << stringPtr << """ << endl;
16  }

The string "51.2% are admitted" is converted to the
double value 51.2 and the string "% are admitted"

Function strtol (Fig. 22.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 11 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.

Fig. 22.32 String-conversion function strtol.

Alternate View

 1  // Fig. 22.32: fig22_32.cpp
 2  // Using strtol.
 3  #include <iostream>
 4  #include <cstdlib> // strtol prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char* string1{"-1234567abc"};
 9     char* remainderPtr{nullptr};
10
11     long x{strtol(string1, &remainderPtr, 0)}; // convert to long
12
13     cout << "The original string is "" << string1
14        << ""
The converted value is " << x
15        << "
The remainder of the original string is "" << remainderPtr
16        << ""
The converted value plus 567 is " << x + 567 << endl;
17  }

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. 22.33) converts to unsigned long a sequence of characters representing an unsigned long integer. The function works identically to strtol. Line 12 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.

Fig. 22.33 String-conversion function strtoul.

Alternate View

 1  // Fig. 22.33: fig22_33.cpp
 2  // Using strtoul.
 3  #include <iostream>
 4  #include <cstdlib> // strtoul prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char* string1{"1234567abc"};
 9     char* remainderPtr{nullptr};
10
11     // convert a sequence of characters to unsigned long
12     unsigned long x{strtoul(string1, &remainderPtr, 0)};
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 minus 567 is " << x - 567 << endl;
18  }

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset