美文网首页
ANSI 和 Unicode 互相转换

ANSI 和 Unicode 互相转换

作者: 星星之火666 | 来源:发表于2019-05-11 21:10 被阅读0次
#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;
}

相关文章

网友评论

      本文标题:ANSI 和 Unicode 互相转换

      本文链接:https://www.haomeiwen.com/subject/sspfaqtx.html