Multi-character and wide character conversion methods are often used in Windows. Therefore, multi-character MultiChar is also an ANSI encoding method, while wide character WideChar is also a Unicode encoding method.
The first two traditional conversion methods are MutiByteToWideChar and WideCharToMutiByte.
Int MultiByteToWideChar (
UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr,
Int cchMultiByte,
LPWSTR lpWideCharStr,
Int cchWideChar
);
This is a method for converting multiple characters into wide characters. The six parameters mean the character set corresponding to the multiple characters in CodePage, which is usually CP_ACP. dwFlags (some standard bits are generally not required, set it to 0), lpMultiByteStr multi-character address), cchMultiBytes multi-character string length, equivalent to the number of bytes of the string, if it is 1, the function determines the length ), lpWideCharStr wide character address), cchWideChar wide character length, equivalent to the number of strings ). The above two lengths are the length of '\ n.
If lpWideCharStr is not NULL, the length of the converted string is returned. If lpWideCharStr is NULL, the number of characters returned by lpMultiByteStr is not the string length !!!).
Common usage:
// Set lpWideCharStr to NULL to get the length of szText. Int iBuffSize =: MultiByteToWideChar (CP_ACP, 0, szText,-1, NULL, 0); // judge whether the character length is greater than 0if (iBuffSize> 0) {LPWSTR wszString = new wchar_t [iBuffSize + 1]; int nChars =: MultiByteToWideChar (CP_ACP, 0, szText,-1, wszString, iBuffSize); // This is a worry about conversion failure, so that nChars is 0, and wszString is set to an empty string nChars = nChars <iBuffSize? NChars: iBuffSize; wszString [nChars] = 0 ;}
The other is from wide to multi-character:
Int WideCharToMultiByte (
UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cchMultiByte, LPSTR lpDefaultChar, LPBOOL pfUsedDefaultChar );
Here there are two more parameters than MultiByteToWideChar, and the first six parameters are the same. lpDefaultChar is used to replace lpDefaultChar when a function encounters a wide character that cannot be converted, if lpDefaultChar is NULL, use? . PfUsedDefaultChar is used to set pfUsedDefaultChar to TRUE if one character fails to be converted. Otherwise, pfUsedDefaultChar is set to FALSE.
int iBuffSize = ::WideCharToMultiByte(CodePage, 0, szString, -1, NULL, 0, NULL, false);if (iBuffSize > 0 ){ m_pString = new char[iBuffSize]; ::WideCharToMultiByte(CodePage, 0, szString, -1, m_pString, iBuffSize, NULL, false);}
The other two conversion methods are A2W and W2A, which respectively indicate multi-character to wide character and wide character to multi-character. These two functional atl functions need to be added with the header file <atlconv. h>
USES_CONVERSION; wchar_t wszText [] = L "1. unicode character conversion to ANSI; "; char szText [] =" 2. ANSI character conversion to Unicode. "; printf (" % s \ n ", W2A (wszText); wprintf (L" % s \ n ", A2W (szText ));
Note that USES_CONVERSION must be added before each conversion.
However, do not use W2A or A2W in large loops or very long functions because of memory overflow.