两个函数定义:
int MultiByteToWideChar(
UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar );
int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar );
CodePage
[in] Code page to be used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system.
The following table shows possible values.
Value | Description |
---|---|
CP_ACP | ANSI code page |
CP_MACCP | Not supported |
CP_OEMCP | OEM code page |
CP_SYMBOL | Not supported |
CP_THREAD_ACP | Not supported |
CP_UTF7 | UTF-7 code page |
CP_UTF8 | UTF-8 code page |
MultiByteToWideChar
-
lpMultiByteStr
-
[in] Pointer to the character string to be converted.
-
cbMultiByte
-
[in] Size, in bytes, of the string pointed to by the lpMultiByteStr parameter. If this value is –1, the string is assumed to be null terminated and the length is calculated automatically.
-
lpWideCharStr
-
[out] Pointer to a buffer that receives the translated string.
-
cchWideChar
-
[in] Size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter.
If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer
WideCharToMultiByte
-
lpWideCharStr
-
[in] Pointer to the wide-character string to be converted.
-
cchWideChar
-
[in] Number of Unicode (16-bit) characters in the string pointed to by the lpWideCharStr parameter. If this value is –1, the string is assumed to be null-terminated and the length is calculated automatically.
-
lpMultiByteStr
-
[out] Pointer to the buffer to receive the translated string.
-
cbMultiByte
-
[in] Size, in bytes, of the buffer pointed to by the lpMultiByteStr parameter. If this value is zero, the function returns the number of bytes required for the buffer. (In this case, the lpMultiByteStr buffer is not used.)
-
lpDefaultChar
-
[in] Pointer to the character used if a wide character cannot be represented in the specified code page. If this parameter is NULL, a system default value is used.
The function is faster when both lpDefaultChar and lpUsedDefaultChar are NULL.
-
lpUsedDefaultChar
-
[in, out] Pointer to a flag that indicates whether a default character was used.
The flag is set to TRUE if one or more wide characters in the source string cannot be represented in the specified code page. Otherwise, the flag is set to FALSE.
This parameter can be NULL.
另外,自己写了一段常用的转换代码
#include<iostream> #include <Windows.h> #include<string> using namespace std; wstring ANSIToUnicode( const string& str ) { int unicodeLen = ::MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0); wchar_t *pUnicode = new wchar_t[unicodeLen+1]; MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen); pUnicode[unicodeLen] = '\0'; wstring ret = ( wchar_t* )pUnicode; delete pUnicode; return ret; } string UnicodeToANSI( const wstring& str ) { // wide char to multi char int ANSILen = WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL); char* pANSI = new char[ANSILen + 1]; WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,pANSI,ANSILen,NULL,NULL); pANSI[ANSILen] = '\0'; string strret = pANSI; delete[] pANSI; return strret; } wstring UTF8ToUnicode( const string& str ) { int unicodeLen = MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0); wchar_t * pUnicode = new wchar_t[unicodeLen+1]; MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen); pUnicode[unicodeLen] = '\0'; wstring ret = ( wchar_t* )pUnicode; delete pUnicode; return ret; } int main(void) { string str1 = "中国"; wstring wstr1 = ANSIToUnicode(str1); wcout.imbue(locale("chs")); wcout<<wstr1<<endl; wstring wstr2(L"中国"); string str2= UnicodeToANSI(wstr2); cout<<str2<<endl; return 0; }