Unicode转多字节:WideCharToMultiByte
多字节转Unicode:MultiByteToWideChar
代码演示
#include <windows.h> int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
BOOL b;
WCHAR szWString[] = L"Unicode宽字符:爱白菜的小昆虫", szWBuff[];
CHAR szAString[] = "多字节窄字符:爱白菜的小昆虫", szABuff[];
TCHAR szString[] = TEXT("自适应字符:爱白菜的小昆虫"), szBuff[]; //Unicode 宽字符
MessageBoxW(NULL, szWString, L"Unicode显示", MB_OK);
//Unicode转多字节
WideCharToMultiByte( CP_ACP,
WC_COMPOSITECHECK,
szWString, //Unicode字节缓冲区
lstrlenW(szWString)+,//Unicode字节字符需要转换的长度,+1包涵'\0'
szABuff,//接受被转换的字符串缓冲区
sizeof(szABuff),//接受字符串缓冲区最大值
NULL, &b);
MessageBoxA(NULL, szABuff, "Unicode转多字节", MB_OK); //多字节 窄字符
MessageBoxA(NULL, szAString, "多字节显示", MB_OK);
//多字节转Unicode
MultiByteToWideChar(CP_ACP,
MB_PRECOMPOSED,
szAString,//多字节缓冲区
lstrlenA(szAString)+,//需要转换的多字节字符串的长度,+1包涵'\0'
szWBuff,//接受被转的字符串缓冲区
sizeof(szWBuff));//接受字符串缓冲区最大值
MessageBoxW(NULL, szWBuff, L"多字节转Unicode", MB_OK); return ;
}