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、

字符集(编码)转换_Windows的更多相关文章

  1. 字符集(编码)转换_Linux

    ZC: 来自 我的项目 czgj 1.代码: #include <stdio.h> #include <iconv.h> #include <string.h> / ...

  2. 字符集(编码)转换_Qt532_QString

    1.网上的资料: 1.1.参考网址:http://blog.csdn.net/changsheng230/article/details/6588447 1.2.网页内容: “ Qt 使用Unicod ...

  3. 关于JS的编码转换问题

    在进行JS开发过程中,尤其是在开发报表时,报表已集成到Web页面中,通过在页面传递参数至报表中时,会发现有时某些参数值,传递到报表中是显示为问号或乱码等等一系列不能正常显示的情况. 这是由于浏览器和报 ...

  4. 帆软报表FineReport数据库连接编码转换

    1. 问题描述 数据库会以某种编码方式保存与读取数据,FineReport解析时默认使用GBK字符集,若数据库端编码与设计器端编码不一致时,就会导致中文及特殊字符的乱码. FineReport在定义数 ...

  5. java Unicode、ISO-8859-1、GBK、UTF-8编码转换深入浅出

    参考文献:搞懂ASCII, ISO8859-1, ANSI和Unicode Unicode百度文献 ISO-8859-1百度文献 注: 1.utf-8虽然是国际编码,对不同范围的字符使用不同长度的编码 ...

  6. Base64的编码转换方式

    下面,详细介绍Base64的编码转换方式. 所谓Base64,就是说选出64个字符----小写字母a-z.大写字母A-Z.数字0-9.符号"+"."/"(再加上 ...

  7. 各种编码中汉字所占字节数;中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030

    vim settings set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936,latin1set termencoding=utf-8se ...

  8. 理解perl的编码转换——utf8以及乱码

    工作需要,闲暇之余,仔细研究了一下脚本乱码的问题 1. vim新建的文件 1)在linux命令行 vim命令建立的文件,如果内容中不出现中文,默认是ASCII.那么用notepad++打开的时候,就是 ...

  9. iconv字符编码转换

    转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...

随机推荐

  1. c#实现图片二值化例子(黑白效果)

    C#将图片2值化示例代码,原图及二值化后的图片如下: 原图: 二值化后的图像: 实现代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...

  2. matplotlib--画图时保存图片空白的问题

    问题: 当使用如下代码保存使用 plt.savefig 保存生成的图片时,结果打开生成的图片却是一片空白. import matplotlib.pyplot as plt ""&q ...

  3. JS在不同js文件中互相调用

    例如有这样一个html,里面有一个按钮,当按下时调用b.js文件中的方法b().而b()中又要调用a.js文件中的方法a().若要实现这个功能,必须注意,将要引入的Js文件代码放在</body& ...

  4. win 7 和 winserver 2008 下,布署网站遇到的错误解决方法

    本人亲测,有效. 1.如果只列出目录: web.config的system.webServer配置节下是否有这个: <modules runAllManagedModulesForAllRequ ...

  5. GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/gradle-3

    Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/gradle-3 ...

  6. Redis 十分钟快速入门

    本教程是一个快速入门教程,所以Redis的命令只是简单介绍了几个常用的,如果有其他需求请求官网查看API 使用. 1. Redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的ke ...

  7. php7安装memchced扩展

    tar -zxvf memcached-3.0.3.tgz cd memcached-3.0.3 phpize yum install libmemcached libmemcached-dev ./ ...

  8. python使用WSGI接口实现简单网页

    Python Web 介绍 Python的Web服务器分为服务器程序和应用程序.服务器程序负责接收客户端的请求发送给应用程序,应用程序负责处理请求返回给服务器程序.为了方便应用程序的开发,我们把常用的 ...

  9. cygwin本地.bashrc配置

    echo -e "====================================================================================== ...

  10. php PDO遇到的坑

    <?php $dbConn = new PDO( "mysql:host=localhost;dbname=adtuu",'root','root', array( // 强 ...