CMarkup AToUTF8 Method

static MCD_STR CMarkup::AToUTF8( MCD_CSTR pszANSI );

AToUTF8 converts an ANSI string to UTF-8, returning the UTF-8 string. You must #include <locale.h> and call setlocale(LC_ALL, "") (or similar) somewhere in your program to enable the system ANSI code page in the C++ multibyte functions.

There is also a corresponding UTF8ToA method for converting in the other direction. These routines are only compiled in the non-UNICODE build, i.e. if UNICODE is not defined. As described in ANSI and Unicode files and C++ strings, these functions make it more convenient to keep a document in memory in UTF-8, converting to and from ANSI for Win32 APIs.

Typically you will use this conversion on string values that come from ANSI Windows functions, not on tag names and values known to be identifiers or numbers. The following example shows values being converted to UTF-8 when put into the document, and converted to ANSI when they are brought out of the document.

CString csName;
wnd.GetWindowText( csName );
CMarkup xml;
xml.AddElem( "A", CMarkup::AToUTF8(csName) );
...
CString csName = CMarkup::UTF8ToA( xml.GetData() );
wnd.SetWindowText( csName );

These UTF-8 to ANSI conversion functions depend on wcstombs and mbstowcs which convert between wide char and locale charset. These are the reason that setlocale(LC_ALL, ".ACP") is needed.