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. 搭建Python3的jupyter notebook服务器

    摘要:搭建Python3 jupyter notebook. 激活Python3后,进入Python交互环境 1. 登陆远程服务器 2. 生成配置文件 1. $jupyter notebook --g ...

  2. python性能分析(一)——使用timeit给你的程序打个表吧

    前言 我们可以通过查看程序核心算法的代码,得知核心算法的渐进上界或者下界,从而大概估计出程序在运行时的效率,但是这并不够直观,也不一定十分靠谱(在整体程序中仍有一些不可忽略的运行细节在估计时被忽略了) ...

  3. python3 列表的常用方法

    Python3中常用的列表方法(method) 见:help(list) 方法 意义 L.index(v [, begin[, end]]) 返回对应元素的索引下标, begin为开始索引,end为结 ...

  4. Impala shell详解

    不多说,直接上干货! 查看帮助文档 impala-shell -h 刷新整个云数据 impala-shell -ruse impala;show tables; 去格式化,查询大数据量时可以提高性能 ...

  5. WireShark 基本介绍

    文中内容主要转自:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html 一.Wireshark 与 Fiddler 比较: F ...

  6. redis删除单个key和多个key,ssdb会落地导致重启redis无法清除缓存

    redis删除单个key和多个key,ssdb会落地导致重启redis无法清除缓存,需要针对单个key进行删除 删除单个:del key 删除多个:redis-cli -a pass(密码) keys ...

  7. 开源|如何使用CNN将视频从2D到3D进行自动转换(附源代码)

    http://www.sohu.com/a/128924237_642762 全球人工智能 文章来源:GitHub 作者:Eric Junyuan Xie 它是如何运行的? 在运行代码之前,请先根据官 ...

  8. 定制django admin页面的跳转

    在django admin的 change_view,  add_view和delete_view页面,如果想让页面完成操作后跳转到我们想去的url,该怎么做 默认django admin会跳转到ch ...

  9. 09: python基础补充

    1.1 闭包 1.闭包概念 1. 在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用,这样就构成了一个闭包 2. 一般情况下,在我们认知当中,如果一个函数 ...

  10. 20145324王嘉澜《网络对抗技术》Web基础

    实践要求 ①Web前端HTML: 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML ②Web前端javascipt: 理解JavaScript ...