The IString class provides a number of as... functions that convert from IString objects to numeric types. You can also convert from numeric types to IString objects by using the versions of the IString constructor that take numeric values as arguments. The following example shows various IString functions that convert between strings and numbers:
// Conversion between IString and numeric values
#include <iostream.h> #include <istring.hpp>
void main() {
IString NumStr=1.4512356919E1; // Initialized with a float value
int Integer=NumStr.asInt(); // Convert to integer value
float Float=NumStr.asDouble(); // C++ conversion rules allow asDouble's
// result to be converted to float
double Double=NumStr.asDouble(); // Convert to double value
NumStr=688; // Assign another integer value
cout.precision(20); // Set precision of cout stream
cout << "Integer: " << Integer << "\nFloat: " << Float
<< "\nDouble: " << Double << "\nString: " << NumStr << endl;
}
This program produces the following output:
Integer: 14 Float: 14.512356758117676 Double: 14.512356919 String: 688
You can also change the base notation of IString objects containing integer numbers, by using the d2b, d2x or d2c functions, which convert from decimal to binary, hexadecimal, or character representations.