ANSI 和 Unicode 互相转换

2019-05-11  本文已影响0人  星星之火666
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    wchar_t* ws = (wchar_t*)L"测试字符串";
    char* ss = (char*)"ABC我们";

    int bufSize = WideCharToMultiByte(CP_ACP, NULL, ws, -1, NULL, 0, NULL, FALSE);
    cout << bufSize << endl;
    char* sp = new char[bufSize];
    WideCharToMultiByte(CP_ACP, NULL, ws, -1, sp, bufSize, NULL, FALSE);
    cout << sp << endl << endl;
    delete[] sp;

    bufSize = MultiByteToWideChar(CP_ACP, 0, ss, -1, NULL, 0);
    cout << bufSize << endl;
    wchar_t* wp = new wchar_t[bufSize];
    MultiByteToWideChar(CP_ACP, 0, ss, -1, wp, bufSize);
    wcout.imbue(locale("chs"));
    wcout << wp;
    delete[] wp;
}
上一篇 下一篇

猜你喜欢

热点阅读