MFC Unicode character set and multi-byte character set the complete end of the problem!

Source: Internet
Author: User

Unicode and Multibyte Character set (MBCS) support Visual Studio. NET 2003

Some international markets use languages such as Japanese and Chinese in large character sets. To support programming in these markets, the Microsoft Foundation Class Library (MFC) supports the processing of large character sets in two ways:

    • Unicode
    • Multibyte Character set (MBCS)

Unicode and multibyte character sets are two different ways of encoding, and some of the corresponding functions are incompatible under different encoding methods.

After reading a multi-byte menu string, use MultiByteToWideChar () to convert to a Unicode string

Or use the version a of the API to set the display name of the menu, for example, Setmenuiteminfoa (...)

If your new library is Unicode, it is developed in Unicode, with no multibyte encoding, and there are too many drawbacks to multibyte coding.
In fact, it is very simple to change, you have all the char changed to TCHAR, if you can use CString, try to use CString. All string functions are replaced with _t versions, such as _tsprintf (), _tcscpy, etc... Because these things are compatible with both Unicode and ANSI, it is easy to change them.

You just need to use WideCharToMultiByte to convert a string when a parameter is passed to a few multibyte DLL functions.

When we first began to learn MFC, there was no doubt that we would encounter such problems.

VC6 settings: Multibyte. The default setting for VS: Unicode, which can be changed to multibyte in attributes. Multi-byte encoding: Char,string,cstringa. Unicode encoding: Wchar_t,wstring,cstringw. Note: If the item is Unicode, then CString is defined as CSTRINGW, otherwise it is cstringa. Attached: LPSTR = CHAR*;LPCSTR = Const CHAR*;LPWSTR = WCHAR_T*;LPCWSTR = Const wchar_t* As for LPCTSTR, it is also defined as LPCWSTR (Unicode) or LPCSTR (multibyte) according to the project character set. OK, the definition above is obvious, do not confuse when using, I think the best way is to use wchar_t and wstring (API mostly use Unicode, even multi-byte version of the multi-byte to Unicode and then to call the Unicode version of the API), I don't like CString very much. Next is the conversion: www.2cto.com//c++char* to String:string str ("char"); or string str = "char"; string to char*:const char* ch = str.c_str (); wchar_t* and wstring are the same as above. mfcchar* to Cstringa:cstringa str ("char"); CStringA to char*:const char* ch = str.operator LPCSTR (); (Note: Against the use of GetBuffer, because the use of this, must be releasebuffer, and many times there is no need to allocate space) wchar_t* and CStringW are the same. Convert between character sets char* to wchar_t*:ca2w a2w ("char");  const wchar_t* wch = A2w.operator lpcwstr (); wchar_t* to char*:cw2a w2a (L "wchar_t"); CONST char* ch = w2a.operator LPCSTR ();   (note: Against the use of _t ("CHS") this way, the program character set should be determined at the outset, and preferably Unicode) about ca2w and cw2a These two classes, you can find the source code in Atlconv.h, in fact, is the encapsulation of WideCharToMultiByte and MultiByteToWideChar.   http://bbs.esrichina-bj.cn/esri/thread-97480-1-1.html

------Solution--------------------

After reading a multi-byte menu string, use MultiByteToWideChar () to convert to a Unicode string

Or use the version a of the API to set the display name of the menu, for example, Setmenuiteminfoa (...)

------Solution--------------------
If your new library is Unicode, it is developed in Unicode, with no multibyte encoding, and there are too many drawbacks to multibyte coding.
In fact, it is very simple to change, you have all the char changed to TCHAR, if you can use CString, try to use CString. All string functions are replaced with _t versions, such as _tsprintf (), _tcscpy, etc... Because these things are compatible with both Unicode and ANSI, it is easy to change them.
char * ch_a = (char*) str. GetBuffer (str. GetLength ());

Reference documents:

https://msdn.microsoft.com/zh-cn/library/cc468203 (v=vs.71). aspx

Ccjt.taobao.com

MFC Unicode character set and multi-byte character set the complete end of the problem!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.