Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

3.8 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 a wchar_t.

Tools.h++ 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>
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 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 <rw/cstring.h>
#include <rw/wstring.h>
#include <assert.h>
main() {
RWCString EnglishSun("Sunday");                   // Ascii string
assert(EnglishSun.isAscii());                               // OK

// Now convert from Ascii to wide characters:
RWWString wEnglishSun(EnglishSun, RWWString::ascii);

assert(wEnglishSun.isAscii());                              // OK
RWCString check = wEnglishSun.toAscii();
assert(check==EnglishSun);                                  // OK
return 0; }

Note how the member functions RWCString::isAscii() and RWWString::isAscii() are used to ensure that the strings consist entirely of Ascii characters. The RWWString constructor:

RWWString(const char*, ascii_);

is used to convert from Ascii to wide characters. The parameter ascii_ is an enum with a single possible value, ascii.

The member function RWWString::toAscii() is used to convert back.



Previous fileTop of documentContentsIndexNext file
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.