zoukankan      html  css  js  c++  java
  • 字符集(编码)转换_Windows

    ZC: 来自 我的项目 czgj

    ZC: (1)、经过测试 MultiByteToWideChar(...) 返回的是 (需要的)WideChar[宽字符]的个数;(2)、WideCharToMultiByte 返回的是 (需要的)Char[窄字符]的个数 。

    1、代码:

    #include "TzEncodingWindows.h"
    
    
    #ifdef BUILD_IN_WIN32
        #include <QDebug>
    
        #include <windows.h>
        #include <wchar.h>
    
        #include <string>
        #include <iostream>
        using namespace std;
    #endif// BUILD_IN_WIN32
    
    
    TzEncodingWindows::TzEncodingWindows()
    {
    
    }
    
    TzEncodingWindows::~TzEncodingWindows()
    {
    
    }
    
    
    #ifdef BUILD_IN_WIN32
    
    //*
    // ZC: 下面的代码来自网络:// http://www.cnblogs.com/lidabo/p/3903620.html
    
            // ZC: Ansi-->Unicode
            std::wstring MBytesToWString(const char* lpcszString)
            {
                //int len = strlen(lpcszString);
                int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, NULL, 0);
                wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
                memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
                ::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
                wstring wString = (wchar_t*)pUnicode;
                delete [] pUnicode;
                return wString;
            }
    
            // ZC: Unicode-->Ansi
            std::string WStringToMBytes(const wchar_t* lpwcszWString)
            {
                char* pElementText;
                int iTextLen;
                // wide char to multi char
                iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, NULL, 0, NULL, NULL);
                pElementText = new char[iTextLen + 1];
                memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
                ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, 0, pElementText, iTextLen, NULL, NULL);
                std::string strReturn(pElementText);
                delete [] pElementText;
                return strReturn;
            }
    
            // ZC: Utf8-->Unicode
            std::wstring UTF8ToWString(const char* lpcszString)
            {
                //int len = strlen(lpcszString);
                int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, NULL, 0);
                wchar_t* pUnicode;
                pUnicode = new wchar_t[unicodeLen + 1];
                memset((void*)pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
                ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
                wstring wstrReturn(pUnicode);
                delete [] pUnicode;
                return wstrReturn;
            }
    
            // ZC: Unicode-->Utf8
            std::string WStringToUTF8(const wchar_t* lpwcszWString)
            {
                char* pElementText;
                int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, NULL, 0, NULL, NULL);
                pElementText = new char[iTextLen + 1];
                memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
                ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, pElementText, iTextLen, NULL, NULL);
                std::string strReturn(pElementText);
                delete [] pElementText;
                return strReturn;
            }
    //*/
    
    
    
    
    
    void TzEncodingWindows::Test_Ansi2Utf8()
    {
    
        qDebug() << "Hello : "+QString::number(sizeof(char));
    //*
        char* lpcszString = "50路";
        //::MessageBoxA(0, lpcszString, "MessageBoxA - 1", 0);
    
        QString str = QString::fromUtf8(lpcszString);
        qDebug() << "lpcszString : "+QString::fromUtf8(lpcszString);
        qDebug() << "str.length() : "+ QString::number(str.length());
    
        QByteArray byteArr = str.toUtf8();
        qDebug() << "byteArr.length() : "+QString::number(byteArr.length());
    
        for (int i=0; i<byteArr.length(); i++)
        {
            qDebug() << QString::number(i)+" : "+QString::number((int)byteArr.at(i), 16);
        }
    
        qDebug() << "strlen(lpcszString) : "+QString::number(strlen(lpcszString));
    
    
    
    
        int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, NULL, 0);
        qDebug() << "unicodeLen : "+QString::number(unicodeLen);
        wchar_t* pUnicode;
        pUnicode = new wchar_t[unicodeLen + 1];
        memset((void*)pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
        ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
    
        //::MessageBoxW(0, pUnicode, L"MessageBoxW", 0);
        wchar_t* lpwcszWString = pUnicode;
        ::MessageBoxW(0, lpwcszWString, L"MessageBoxW", 0);
    
        int iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, NULL, 0, NULL, NULL);
        qDebug() << "iTextLen : "+QString::number(iTextLen);
        char* pElementText = new char[iTextLen + 1];
        memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
        int iRtn = ::WideCharToMultiByte(
                    CP_ACP,
                    0,
                    lpwcszWString,
                    unicodeLen,//0, // ZC: 网页中的这个值,设置的不正确,导致函数返回0,且GetLastError为ERROR_INVALID_PARAMETER。Fuck !!!
                    pElementText,
                    iTextLen,
                    NULL,
                    NULL);
        // ERROR_INVALID_PARAMETER --> 87
        qDebug() << "WideCharToMultiByte return : "+QString::number(iRtn)+" -> GetLastError : "+QString::number(::GetLastError());
        ::MessageBoxA(0, pElementText, "MessageBoxA", 0);
    
        for (int i=0; i<iTextLen; i++)
        {
            qDebug() << QString::number(i)+" --> "+QString::number((int)pElementText[i], 16);
        }
    
        std::string strReturn(pElementText);
    
    
        qDebug() << "strReturn : " + QString::fromStdString(strReturn);
    
    //    delete [] pUnicode;
    //    delete [] pElementText;
    //*/
    }
    
    #endif// BUILD_IN_WIN32

    2、

  • 相关阅读:
    Gitlab的安装
    转 Java操作PDF之iText详细入门
    ElasticSearch聚合(转)
    谷歌搜索技巧(转)https://www.runningcheese.com/google
    自学elastic search
    WinForm richtextbox 关键字变红色
    https采集12306(复制)
    LTS
    学习Android MediaPlayer
    UML基础知识
  • 原文地址:https://www.cnblogs.com/cppskill/p/5532793.html
Copyright © 2011-2022 走看看