Using the Serial Package with the Internationalization Module

The Serial package of the Advanced Tools Module provides an RWUString inserter and extractor interface to object streams.

To use this interface, include the header file RWObjectStreamI18n.h and then use operator<< or operator>> to shift RWUStrings in or out of compact object streams. The RWUString interface takes and produces UTF-16 encoded characters. The Streams package of the Advanced Tools Module and the Internationalization Module provide methods for converting to and from UTF-16 as shown in the next two sections.

Converting from a Local Encoding to UTF-16

To provide a convenient method for translation between character encodings, RWUString works directly with character-encoding converters in the Internationalization Module.

For example, if you want to write out to UTF-16 a string encoded in the Japanese Shift-JIS, you would use the following.

#include "rw/i18n/RWUToUnicodeConverter.h" // 1

RWCString myString;

// Initialize myString with characters encoded in Shift-JIS

RWUToUnicodeConverter fromShiftJis("Shift-JIS"); // 2

RWByteOutputStream& tmpBStream =

RWByteToStreambufOutputStreamImp::make(cout.rdbuf());

RWDataOutputStream tmpDStream =

RWNativeDataToByteOutputStreamImp::make(tmpBStream);

RWObjectOutputStream out =

RWCompactObjectOutputStreamImp::make(tmpDStream); // 3

out << RWUString(myString, fromShiftJis); // 4

// 1  Include the header file for the “to Unicode” converter class.

// 2   Create a converter for converting from Shift-JIS to UTF-16.

// 3   Create the compact object output stream.

// 4   Using the constructor that takes a second “converter” parameter, construct an RWUString from the RWCString containing characters encoded in Shift-JIS. The Shift-JIS characters will be converted to UTF-16 as part of the construction. Then shift out the RWUString.

Converting from UTF-16 to a Local Encoding

Similarly, if you want to read in a document containing a string encoded in UTF-16, and then convert the string to Shift-JIS, use the following code.

#include "rw/i18n/RWUFromUnicodeConverter.h"

RWUFromUnicodeConverter toShiftJis("Shift-JIS");

RWByteInputStream& tmpBStream =

RWByteFromStreambufInputStreamImp::make(cin.rdbuf());

RWDataInputStream tmpStream =

RWNativeDataFromByteInputStreamImp::make(tmpBStream);

RWObjectInputStream in =

RWCompactObjectInputStreamImp::make(tmpStream);

RWUString someRWUString;

in >> someRWUString;

someRWUString.toBytes(toShiftJis); // 1

// 1  Use the toBytes() method of the RWUString to convert from UTF-16 to Shift-JIS.