windows程序设计 Unicode和多字节之间转换

Unicode转多字节:WideCharToMultiByte

多字节转Unicode:MultiByteToWideChar

代码演示

#include <windows.h>

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
    )
{
    BOOL b;
    WCHAR szWString[120] = L"Unicode宽字符:爱白菜的小昆虫", szWBuff[120];
    CHAR szAString[120] = "多字节窄字符:爱白菜的小昆虫", szABuff[120];
    TCHAR szString[120] = TEXT("自适应字符:爱白菜的小昆虫"), szBuff[120];

    //Unicode 宽字符
    MessageBoxW(NULL, szWString, L"Unicode显示", MB_OK);
    //Unicode转多字节
    WideCharToMultiByte( CP_ACP, 
        WC_COMPOSITECHECK, 
        szWString, //Unicode字节缓冲区
        lstrlenW(szWString)+1,//Unicode字节字符需要转换的长度,+1包涵''
        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,//需要转换的多字节字符串的长度,+1包涵''
        szWBuff,//接受被转的字符串缓冲区
        sizeof(szWBuff));//接受字符串缓冲区最大值
    MessageBoxW(NULL, szWBuff, L"多字节转Unicode", MB_OK);
    
    return 0;
}
原文地址:https://www.cnblogs.com/xuqiulin/p/10176454.html