Converting between Strings and Different Base Notations

You can use the format conversion functions to change the way the data in a string is represented. These functions are overloaded so that each function has two versions. The nonstatic version replaces the value of the string with the converted value. The static version preserves the original string and returns a new string object containing the converted value. For example:

aString.c2b();                                       // Changes value of aString
IString binaryDigits = IString::c2b( aString );      // Preserves value of aString

The conversion functions check the format of the source string to make sure it is compatible with the source format implied by the function name. For example, if you use the b2d function to convert a string from binary to decimal, the function first checks that the string contains only the digits '0' and '1'. If it contains any characters other than those allowed by the source type, the format conversion functions always return 0.

The following example shows the use of the conversion functions. If you examine both the example and the output provided below, you can see how to use the functions.

// IString conversion functions
#include <istring.hpp>
#include <iostream.h>
enum Bases {Bin, Dec, Hex, Char};
IString Base[4]={"binary", "decimal", "hex", "character"};
IString NumStr; 
void Show(int From, int To, IString& Result) {
   cout << NumStr << " in " << Base[From] << " is "
        << Result << " in " << Base[To] << '.' << endl;
   } 
void main() {
   IString NewStr;
   NumStr="122";
      NewStr=IString::d2b(NumStr); Show(Dec,Bin,NewStr);
      NewStr=IString::d2x(NumStr); Show(Dec,Hex,NewStr);
      NewStr=IString::d2c(NumStr); Show(Dec,Char,NewStr); 
   NumStr="Hat";
      NewStr=IString::c2b(NumStr); Show(Char,Bin,NewStr);
      NewStr=IString::c2d(NumStr); Show(Char,Dec,NewStr);
      NewStr=IString::c2x(NumStr); Show(Char,Hex,NewStr); 
   NumStr="5F";
      NewStr=IString::x2b(NumStr); Show(Hex,Bin,NewStr);
      NewStr=IString::x2d(NumStr); Show(Hex,Dec,NewStr);
      NewStr=IString::x2c(NumStr); Show(Hex,Char,NewStr); 
   NumStr="0110100001101001";
      NewStr=IString::b2d(NumStr); Show(Bin,Dec,NewStr);
      NewStr=IString::b2x(NumStr); Show(Bin,Hex,NewStr);
      NewStr=IString::b2c(NumStr); Show(Bin,Char,NewStr);
   } 

The output from this program resembles the following. Depending on the code page and character set (ASCII or EBCDIC) of the system you are running the program on, the values may vary.

122 in decimal is 01111010 in binary.
122 in decimal is 7A in hex.
122 in decimal is z in character.
Hat in character is 010010000110000101110100 in binary.
Hat in character is 4743540 in decimal.
Hat in character is 486174 in hex. 
5F in hex is 01011111 in binary.
5F in hex is 95 in decimal.
5F in hex is ¬ in character.
0110100001101001 in binary is 26729 in decimal.
0110100001101001 in binary is 6869 in hex.
0110100001101001 in binary is ΗΡ in character.