How to Handle Object Pascal AnsiString Code Page Specification in C++
The C++ implementation of the
AnsiString type provides CodePage support similar to that available in Object Pascal. However, while the Object Pascal version is implemented via extension to the language, the C++ version is implemented via a template with a non-type parameter as
codepage, as in:
template AnsiStringT<unsigned short codePage>
Therefore, the type previously known as
AnsiString type is now simply a typedef of the new
AnsiStringT template, as in:
typedef AnsiStringT<65001> UTF8String; typedef AnsiStringT<65535> RawByteString;
You can declare other specializations of
AnsiStringT with the code page you need.
All assignments to an
AnsiStringT instance will encode the data in the type's code page. For example, in the following code, the Unicode data assigned to the
UTF8String type variable is automatically UTF8-encoded:
const wchar_t* data = L"СЛАДКОЕ"; UTF8String utfs(data);
utfs variable can be passed to a function expecting a
UnicodeString and the original data will be restored without any loss incurred:
Button1->Caption = utfs; // Set button caption to "СЛАДКОЕ"
Note that while the
AnsiStringT handles the
codepage support behind the scene, you can still explicitly set the code page of an instance by calling the following method:
AnsiStringT<CP>::SetCodePage(unsigned short codePage, bool convert=true)
This method should be used carefully because it might mislead users of the instance who expect the type to contain data encoded in its default code page.
Note: The DefaultSystemCodePage is 0.