编码转换

1、

TzEncodingWindows.cpp

#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、

TzEncodingLinux.cpp

#include "TzEncodingLinux.h"

#include <stdio.h>
#include <iconv.h>
#include <string.h>

//#pragma comment(lib, "iconv.lib") // ZC: 改放到 .pro文件中“LIBS += -liconv”

TzEncodingLinux::TzEncodingLinux()
{

}

TzEncodingLinux::~TzEncodingLinux()
{

}


/*
//此函数说明将要进行哪两种编码的转换,tocode是目标编码,fromcode是原编码,该函数返回一个转换句柄,供以下两个函数使用。
(1) iconv_t iconv_open(const char *tocode, const char *fromcode);

//此函数从inbuf中读取字符,转换后输出到outbuf中,inbytesleft用以记录还未转换的字符数,outbytesleft用以记录输出缓冲的剩余空间。
(2) size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);

//此函数用于关闭转换句柄,释放资源。
(3) int iconv_close(iconv_t cd);
*/

// - - - - - - - 以下是在Linux上实现的字符集转换函数
//int code_convert(char *from_charset,char *to_charset,const char *inbuf, size_t inlen,char *outbuf, size_t outlen)
int code_convert(char *from_charset,char *to_charset, char *inbuf, size_t inlen,char *outbuf, size_t outlen)
{
    iconv_t cd;
    //const char **pin = &inbuf;
    char **pin = &inbuf;
    char **pout = &outbuf;

    cd = iconv_open(to_charset,from_charset);
    if (cd==0) return -1;
    memset(outbuf,0,outlen);
    if (iconv(cd, pin, &inlen,pout, &outlen)==((size_t)-1))
        return -1;
    iconv_close(cd);
    return 0;
}

/* UTF-8 to UTF-16  */
//int u2g(const char *inbuf, size_t inlen, char *outbuf, size_t outlen)
int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
    return code_convert("UTF-8","UTF-16",inbuf,inlen,outbuf,outlen);
}

/* UTF-16 to UTF-8 */
//int g2u(const char *inbuf, size_t inlen, char *outbuf, size_t outlen)
int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
    return code_convert("UTF-16", "UTF-8", inbuf, inlen, outbuf, outlen);
}
// - - - - - - - 以上是在Linux上实现的字符集转换函数

知识点来自网络,具体哪个网页 忘掉了...

代码来自“czgj”,代码保存于:百度云

原文地址:https://www.cnblogs.com/CodeSkill/p/5082778.html