Strings: Unicode and Multibyte Character Set (MBCS) Support
Home | Overview | How Do I
Some international markets use languages, such as Japanese and Chinese, with large character sets. To support programming for these markets, the Microsoft Foundation Class Library (MFC) is enabled for two different approaches to handling large character sets:
Unicode
Multibyte Character Sets (MBCS)
MFC Support for Unicode Strings
The entire class library is conditionally enabled for Unicode characters and strings. In particular, class CString is Unicode-enabled.
Note The Unicode versions of the MFC libraries are not copied to your hard drive unless you select them during a Custom installation. They are not copied during other types of installation. If you attempt to build or run an MFC Unicode application without the MFC Unicode files, you may get errors.
To copy the files to your hard drive, rerun Setup, choose Custom installation, clear the check boxes for all other components except "Microsoft Foundation Class Libraries," click the Details button, and select both "Static Library for Unicode" and "Shared Library for Unicode." This will copy the following files to your hard drive:
UAFXCW.LIB UAFXCW.PDB UAFXCWD.LIB UAFXCWD.PDB
MFCxxU.LIB MFCxxU.DBG MFCxxU.DLL MFCxxUD.LIB
MFCxxUD.PDB MFCxxUD.DLL MFCDxxUD.LIB MFCDxxUD.PDB
MFCDxxUD.DLL MFCNxxUD.LIB MFCNxxUD.PDB MFCNxxUD.DLL
MFCOxxUD.LIB MFCOxxUD.PDB MFCOxxUD.DLL
Where xx represents the version number of the file; for example, ‘42’ represents version 4.2.
CString is based on the TCHAR data type. If the symbol _UNICODE is defined for a build of your program, TCHAR is defined as type wchar_t, a 16-bit character encoding type; otherwise, it is defined as char, the normal 8-bit character encoding. Under Unicode, then, CStrings are composed of 16-bit characters. Without Unicode, they are composed of characters of type char.
To complete Unicode programming of your application, you must also:
Use the _T macro to conditionally code literal strings to be portable to Unicode.
When you pass strings, pay attention to whether function arguments require a length in characters or a length in bytes. The difference is important if you’re using Unicode strings.
Use portable versions of the C run-time string-handling functions.
Use the following data types for characters and character pointers:
TCHAR Where you would use char.
LPTSTR Where you would use char*.
LPCTSTR Where you would use const char*. CString provides the operator LPCTSTR to convert between CString and LPCTSTR.
CString also supplies Unicode-aware constructors, assignment operators, and comparison operators.
For related information on Unicode programming, see Unicode and MBCS and Unicode Topics. The Run-Time Library Reference defines portable versions of all of its string-handling functions. See the category Internationalization.
MFC Support for MBCS Strings
The class library is also enabled for multibyte character sets — specifically for double-byte character sets (DBCS).
Under this scheme, a character can be either one or two bytes wide. If it is two bytes wide, its first byte is a special “lead byte,” chosen from a particular range depending on which code page is in use. Taken together, the lead and “trail bytes” specify a unique character encoding.
If the symbol _MBCS is defined for a build of your program, type TCHAR, on which CString is based, maps to char. It’s up to you to determine which bytes in a CString are lead bytes and which are trail bytes. The C run-time library supplies functions to help you determine this.
Under DBCS, a given string can contain all single-byte ANSI characters, all double-byte characters, or a combination of the two. These possibilities require special care in parsing strings, including CString objects.
Note Unicode string serialization in MFC can read both Unicode and MBCS strings regardless of which version of the application you are running. Because of this, your data files are portable between Unicode and MBCS versions of your program.
CString member functions use special “generic text” versions of the C run-time functions they call, or they use Unicode-aware functions such as lstrlen or lstrcpy. Thus, for example, if a CString function would normally call strcmp, it calls the corresponding generic-text function _tcscmp instead. Depending on how the symbols _MBCS and _UNICODE are defined, _tcscmp maps as follows:
_MBCS defined
_mbscmp
_UNICODE defined
wcscmp
Neither symbol defined
strcmp
Note The symbols _MBCS and _UNICODE are mutually exclusive.
Generic-text function mappings for all of the run-time string-handling routines are detailed in the Run-Time Library Reference. See the category Internationalization.
Similarly, CString member functions are implemented using “generic” data type mappings. To enable both MBCS and Unicode, MFC uses TCHAR for char, LPTSTR for char*, and LPCTSTR for const char*. These result in the correct mappings for either MBCS or Unicode.
Unicode and MBCS Provide Portability
With MFC version 3.0 and later, MFC, including CString, is enabled for both Unicode and Multibyte Character Sets (MBCS). This support makes it easier for you to write portable applications that you can build for either Unicode or ANSI characters. To enable this portability, each character in a CString object is of type TCHAR, which is defined as wchar_t if you define the symbol _UNICODE when you build your application, or as char if not. A wchar_t character is 16 bits wide. (Unicode is available only under Windows NT.) MBCS is enabled if you build with the symbol _MBCS defined. MFC itself is built with either the _MBCS symbol (for the NAFX libraries) or the _UNICODE symbol (for the UAFX libraries) defined.
Note The CString examples in this and the accompanying articles on strings show literal strings properly formatted for Unicode portability, using the _T macro, which translates the literal string to the form
L"literal string"
which the compiler treats as a Unicode string. For example, the following code:
CString strName = _T("Name");
is translated as a Unicode string if _UNICODE is defined or as an ANSI string if not. For more information, see the article Strings: Unicode and Multibyte Character Set (MBCS) Support.
A CString object can store up to INT_MAX (2,147,483,647) characters. The TCHAR data type is used to get or set individual characters inside a CString object. Unlike character arrays, the CString class has a built-in memory allocation capability. This allows CString objects to automatically grow as needed (that is, you don’t have to worry about growing a CString object to fit longer strings).
GetLength Returns the number of characters in a CString object. For multibyte characters, counts each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters.