How to Handle Delphi AnsiString Code Page Specification in C++

From RAD Studio
Jump to: navigation, search

Go Up to Handling Delphi Features in C++Builder Index

The C++ implementation of the AnsiString type provides CodePage support similar to that available in Delphi. However, while the Delphi 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);

The 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.

See Also