
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, , lpcszString, -, NULL, );
wchar_t* pUnicode = new wchar_t[unicodeLen + ];
memset(pUnicode, , (unicodeLen + ) * sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP, , lpcszString, -, (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, , lpwcszWString, -, NULL, , NULL, NULL);
pElementText = new char[iTextLen + ];
memset((void*)pElementText, , (iTextLen + ) * sizeof(char));
::WideCharToMultiByte(CP_ACP, , lpwcszWString, , 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, , lpcszString, -, NULL, );
wchar_t* pUnicode;
pUnicode = new wchar_t[unicodeLen + ];
memset((void*)pUnicode, , (unicodeLen + ) * sizeof(wchar_t));
::MultiByteToWideChar(CP_UTF8, , lpcszString, -, (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, , (LPWSTR)lpwcszWString, -, NULL, , NULL, NULL);
pElementText = new char[iTextLen + ];
memset((void*)pElementText, , (iTextLen + ) * sizeof(char));
::WideCharToMultiByte(CP_UTF8, , (LPWSTR)lpwcszWString, -, 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=; i<byteArr.length(); i++)
{
qDebug() << QString::number(i)+" : "+QString::number((int)byteArr.at(i), );
} qDebug() << "strlen(lpcszString) : "+QString::number(strlen(lpcszString)); int unicodeLen = ::MultiByteToWideChar(CP_UTF8, , lpcszString, -, NULL, );
qDebug() << "unicodeLen : "+QString::number(unicodeLen);
wchar_t* pUnicode;
pUnicode = new wchar_t[unicodeLen + ];
memset((void*)pUnicode, , (unicodeLen + ) * sizeof(wchar_t));
::MultiByteToWideChar(CP_UTF8, , lpcszString, -, (LPWSTR)pUnicode, unicodeLen); //::MessageBoxW(0, pUnicode, L"MessageBoxW", 0);
wchar_t* lpwcszWString = pUnicode;
::MessageBoxW(, lpwcszWString, L"MessageBoxW", ); int iTextLen = ::WideCharToMultiByte(CP_ACP, , lpwcszWString, -, NULL, , NULL, NULL);
qDebug() << "iTextLen : "+QString::number(iTextLen);
char* pElementText = new char[iTextLen + ];
memset((void*)pElementText, , (iTextLen + ) * sizeof(char));
int iRtn = ::WideCharToMultiByte(
CP_ACP,
,
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(, pElementText, "MessageBoxA", ); for (int i=; i<iTextLen; i++)
{
qDebug() << QString::number(i)+" --> "+QString::number((int)pElementText[i], );
} std::string strReturn(pElementText); qDebug() << "strReturn : " + QString::fromStdString(strReturn); // delete [] pUnicode;
// delete [] pElementText;
//*/
} #endif// BUILD_IN_WIN32
2、