static MCD_STR CMarkup::UTF8ToA( MCD_CSTR pszUTF8, int* pnFailed = NULL );
UTF8ToA
converts a UTF-8 string to ANSI, returning the ANSI 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.
Typically you will use this conversion on string values that will be used in ANSI Windows functions, not on tag names and values known to be identifiers or numbers.
Supply the pnFailed
argument if you want to know how many characters failed to convert. The following example shows a value being converted from UTF-8 to ANSI and checking whether any characters were lost in the conversion.
int nFailedChars; CString csName = CMarkup::UTF8ToA( xml.GetData(), &nFailedChars ); if ( nFailedChars ) MessageBox( "The name is not supported in the system code page" ); wnd.SetWindowText( csName );
There is also a corresponding AToUTF8 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.
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.