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. opencv3寻找最小包围矩形在图像中的应用-滚动条

    #include<opencv2/opencv.hpp> #include<iostream> #include<vector> using namespace c ...

  2. CentOS.56安装Redis监控工具RedisLive

    RedisLive是一款开源的基于WEB的reids的监控工具,以WEB的形式展现出redis中的key的情况,实例数据等信息! RedisLive在github上的地址:https://github ...

  3. Jmeter CSV Data Set Config参数化

    在使用Jemeter做压力测试的时候,往往需要参数化用户名,密码以到达到多用户使用不同的用户名密码登录的目的.这个时候我们就可以使用CSV Data Set Config实现参数化登录: 首先通过Te ...

  4. testng入门教程2用TestNG编写测试及执行测试

    编写TestNG测试基本上包括以下步骤: 测试和编写业务逻辑,在代码中插入TestNG的注解.. 添加一个testng.xml文件或build.xml中在测试信息(例如类名,您想要运行的组,等..) ...

  5. Twitter OA prepare: Flipping a bit

    You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one ...

  6. Object-C-复制

    copy 减少对象上下文依赖 copy 创建一个新对象,copy得到的副本对象与原来内容相同,新的对象retain为1,与旧有对象的引用计数无关,旧有对象没有变化 使用 copy 创建出来的对象是不可 ...

  7. 缓存(图像 IO 14.2)

    缓存 如果有很多张图片要显示,最好不要提前把所有都加载进来,而是应该当移出屏幕之后立刻销毁.通过选择性的缓存,你就可以避免来回滚动时图片重复性的加载了. 缓存其实很简单:就是存储昂贵计算后的结果(或者 ...

  8. android实操--练习2

    练习2是实现一个计算器的功能:可以加减乘除:可以倒退,可以清空文本. 下面是效果展示: -----------------------布局------------------------------- ...

  9. C++ 简明教程

    C++是一种系统编程语言.用它的发明者, Bjarne Stroustrup的话来说,C++的设计目标是: 成为“更好的C语言” 支持数据的抽象与封装 支持面向对象编程 支持泛型编程 C++提供了对硬 ...

  10. API和正则表达式

    第一章 String & StringBuilderString类用类final修饰,不能被继承,String字符串被创建后永远无法被改变,但字符串引用可以重新赋值,改变引用的指向java字符 ...