MultiByteToWideCharFilter

From Crypto++ Wiki
Jump to: navigation, search

MultiByteToWideCharFilter was developed during the discussion of intermediate filters. Though a learning tool, the filter is useful on Windows platforms. Filter related details can be found at Filter (Intermediate). The remainder of this topic discusses how to use the filter. The counterpart to MultiByteToWideCharFilter is WideCharToMultiByteFilter.

Sample Programs

Crypto++ does not offer a WideStringSink, so the wide string is placed in a std::string. While the data of the converted string is wide, the termination of the string housing the converted is a std::string's NULL. So an assignment through a cast is not possible.

Pipeline

string narrow="MultiByteToWideCharFilter Test", converted;

StringSource( narrow, true,
    new MultiByteToWideCharFilter(
        new StringSink( converted )
    )
);

// converted is housing the wide character string as a byte[].
//  We must offer the proper the size during construction.
std::wstring wide( (wchar_t*)converted.data(), converted.size()/2 );

// The string has been laundered. It's safe to use as a
//  wide string. We must also use wcout rather than cout.
std::wcout << wide << endl;

Filter Put

Using the filter's Put method is the same as any other filter. Below, the narrow string is inserted twice, with a pipe ('|') separating the strings.

MultiByteToWideCharFilter f;
f.Put( (const byte*)narrow.data(), narrow.size() );
f.Put( '|' );
f.Put( (const byte*)narrow.data(), narrow.size() );

converted.resize( (int)f.MaxRetrievable() );
f.Get( (byte*)converted.data(), converted.size() );

// converted is housing the wide character string as a byte[].
//  We must offer the proper the size during construction.
std::wstring wide( (wchar_t*)converted.data(), converted.size()/2 );

// The string has been laundered. It's safe to use as a
//  wide string. We must also use wcout rather than cout.
std::wcout << wide << endl;

Downloads

MultiByteToWideCharFilter.zip - Crypto++ Filter sample wrapping Window's MultiByteToWideChar - 6KB