Wide Character Strings
Class RWWString, also used in representing various alphabets, is similar to RWCString except it works with wide characters. These are much easier to manipulate than multibyte characters because they are all the same size: the size of wchar_t.
The Essential Tools Module makes it easy to convert back and forth between multibyte and wide character strings. Here's an example of how to do it, built on the Sun example in the previous section:
 
#include <rw/cstring.h>
#include <rw/wstring.h>
#include <assert.h>
int main() {
RWCString Sun("\306\374\315\313\306\374");
RWWString wSun(Sun, RWWString::multiByte); // MBCS to wide string
RWCString check = wSun.toMultiByte();
assert(Sun==check); // OK
return 0;
}
Basically, you convert from a multibyte string to a wide string by using the special RWWString constructor:
 
RWWString(const char*, multiByte_);
The parameter RWWString::multiByte_ is an enum with a single possible value, multiByte, as shown in the example. The multiByte argument ensures that this relatively expensive conversion is not done inadvertently. The conversion from a wide character string back to a multibyte string, using the function toMultiByte(), is similarly expensive.
If you know that your RWCString consists entirely of ASCII characters, you can greatly reduce the cost of the conversion in both directions. This is because the conversion involves a simple manipulation of high-order bits:
 
#include <iostream>
#include <rw/cstring.h>
#include <rw/wstring.h>
 
int main ()
{
RWCString EnglishSun("Sunday"); // Ascii string
// Now convert from Ascii to wide characters:
RWWString wEnglishSun(EnglishSun, RWWString::ascii);
 
RWCString check = wEnglishSun.toAscii();
 
std::cout << std::endl << "Original: " << EnglishSun
<< std::endl << std::endl;
 
std::cout << "Conversion to wide char using constructor: " << wEnglishSun
<< std::endl;
 
std::cout << "Conversion to ascii using member function: " << check
<< std::endl;
 
return 0;
}
The RWWString constructor
 
RWWString(const char*, ascii_);
is used to convert from ASCII to wide characters. The parameter RWWString::ascii_ is an enum with a single possible value, ascii. The member function RWWString::toAscii() is used to convert back.