RAD Studio
ContentsIndex
PreviousUpNext
How to Handle Delphi AnsiString Code Page Specification in C++

The C++ implementation of the AnsiString type provides CodePage support similar to that available in Delphi 2009. 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 codepage you need. 

All assignments to an AnsiStringT instance will encode the data in the type's codepage. For example, in the following code, the Unicode data assigned to the UTF8String 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 codepage 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 codepage.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!