Pointer-Based String-Conversion Functions

In Chapter 8, we discussed several of C++'s most popular pointer-based 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 pointer-based string-conversion functions from the general-utilities library . These functions convert pointer-based strings of characters to integer and floating-point values. Figure 22.21 summarizes the pointer-based string-conversion functions. Note the use of const to declare variable nPtr in the function headers (read from right to left as "nPtr is a pointer to a character constant"). When using functions from the general-utilities library, include the header file.

Figure 22.21. Pointer-based 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.22, line 12) converts its argumenta string that represents a floating-point numberto a double value. The function returns the double value. If the string cannot be convertedfor example, if the first character of the string is not a digitfunction atof returns zero.

Figure 22.22. String-conversion function atof.

1 // Fig. 22.22: fig22_22.cpp 2 // Using atof. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // atof prototype 8 using std::atof; 9 10 int main() 11 { 12 double d = atof( "99.0" ); // convert string to double 13 14 cout << "The string "99.0" converted to double is " << d 15 << " The converted value divided by 2 is " << d / 2.0 << endl; 16 return 0; 17 } // end main  

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

Function atoi (Fig. 22.23, line 12) converts its argumenta string of digits that represents an integerto an int value. The function returns the int value. If the string cannot be converted, function atoi returns zero.

Figure 22.23. String-conversion function atoi.

(This item is displayed on page 1086 in the print version)

1 // Fig. 22.23: Fig22_26.cpp 2 // Using atoi. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // atoi prototype 8 using std::atoi; 9 10 int main() 11 { 12 int i = atoi( "2593" ); // convert string to int 13 14 cout << "The string "2593" converted to int is " << i 15 << " The converted value minus 593 is " << i - 593 << endl; 16 return 0; 17 } // end main  

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

Function atol (Fig. 22.24, line 12) converts its argumenta string of digits representing a long integerto 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.


Figure 22.24. String-conversion function atol.

1 // Fig. 22.24: fig22_24.cpp 2 // Using atol. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // atol prototype 8 using std::atol; 9 10 int main() 11 { 12 long x = atol( "1000000" ); // convert string to long 13 14 cout << "The string "1000000" converted to long is " << x 15 << " The converted value divided by 2 is " << x / 2 << endl; 16 return 0; 17 } // end main  

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

Function strtod (Fig. 22.25) converts a sequence of characters representing a floating-point value to double. Function strtod receives two argumentsa 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 16 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.


Figure 22.25. String-conversion function strtod.

1 // Fig. 22.25: fig22_25.cpp 2 // Using strtod. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strtod prototype 8 using std::strtod; 9 10 int main() 11 { 12 double d; 13 const char *string1 = "51.2% are admitted"; 14 char *stringPtr; 15 16 d = strtod( string1, &stringPtr ); // convert characters to double 17 18 cout << "The string "" << string1 19 << "" is converted to the double value " << d 20 << " and the string "" << stringPtr << """ << endl; 21 return 0; 22 } // end main  

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

Function strtol (Fig. 22.26) converts to long a sequence of characters representing an integer. The function receives three argumentsa 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 16 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 string0 indicates an octal number, 0x indicates hexadecimal and a number from 19 indicates decimal.

Figure 22.26. String-conversion function strtol.

(This item is displayed on page 1088 in the print version)

1 // Fig. 22.26: fig22_26.cpp 2 // Using strtol. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strtol prototype 8 using std::strtol; 9 10 int main() 11 { 12 long x; 13 const char *string1 = "-1234567abc"; 14 char *remainderPtr; 15 16 x = strtol( string1, &remainderPtr, 0 ); // convert characters to long 17 18 cout << "The original string is "" << string1 19 << "" The converted value is " << x 20 << " The remainder of the original string is "" << remainderPtr 21 << "" The converted value plus 567 is " << x + 567 << endl; 22 return 0; 23 } // 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 AZ to represent the values 10 to 35. For example, hexadecimal values can consist of the digits 09 and the characters AF. A base-11 integer can consist of the digits 09 and the character A. A base-24 integer can consist of the digits 09 and the characters AN. A base-36 integer can consist of the digits 09 and the characters AZ. [Note: The case of the letter used is ignored.]


Function strtoul (Fig. 22.27) converts to unsigned long a sequence of characters representing an unsigned long integer. The function works identically to function strtol. Line 17 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.

Figure 22.27. String-conversion function strtoul.

(This item is displayed on pages 1088 - 1089 in the print version)

1 // Fig. 22.27: fig22_27.cpp 2 // Using strtoul. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strtoul prototype 8 using std::strtoul; 9 10 int main() 11 { 12 unsigned long x; 13 const char *string1 = "1234567abc"; 14 char *remainderPtr; 15 16 // convert a sequence of characters to unsigned long 17 x = strtoul( string1, &remainderPtr, 0 ); 18 19 cout << "The original string is "" << string1 20 << "" The converted value is " << x 21 << " The remainder of the original string is "" << remainderPtr 22 << "" The converted value minus 567 is " << x - 567 << endl; 23 return 0; 24 } // 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  

Категории