字符集(编码)转换_Windows
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的更多相关文章
- 字符集(编码)转换_Linux
ZC: 来自 我的项目 czgj 1.代码: #include <stdio.h> #include <iconv.h> #include <string.h> / ...
- 字符集(编码)转换_Qt532_QString
1.网上的资料: 1.1.参考网址:http://blog.csdn.net/changsheng230/article/details/6588447 1.2.网页内容: “ Qt 使用Unicod ...
- 关于JS的编码转换问题
在进行JS开发过程中,尤其是在开发报表时,报表已集成到Web页面中,通过在页面传递参数至报表中时,会发现有时某些参数值,传递到报表中是显示为问号或乱码等等一系列不能正常显示的情况. 这是由于浏览器和报 ...
- 帆软报表FineReport数据库连接编码转换
1. 问题描述 数据库会以某种编码方式保存与读取数据,FineReport解析时默认使用GBK字符集,若数据库端编码与设计器端编码不一致时,就会导致中文及特殊字符的乱码. FineReport在定义数 ...
- java Unicode、ISO-8859-1、GBK、UTF-8编码转换深入浅出
参考文献:搞懂ASCII, ISO8859-1, ANSI和Unicode Unicode百度文献 ISO-8859-1百度文献 注: 1.utf-8虽然是国际编码,对不同范围的字符使用不同长度的编码 ...
- Base64的编码转换方式
下面,详细介绍Base64的编码转换方式. 所谓Base64,就是说选出64个字符----小写字母a-z.大写字母A-Z.数字0-9.符号"+"."/"(再加上 ...
- 各种编码中汉字所占字节数;中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
vim settings set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936,latin1set termencoding=utf-8se ...
- 理解perl的编码转换——utf8以及乱码
工作需要,闲暇之余,仔细研究了一下脚本乱码的问题 1. vim新建的文件 1)在linux命令行 vim命令建立的文件,如果内容中不出现中文,默认是ASCII.那么用notepad++打开的时候,就是 ...
- iconv字符编码转换
转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...
随机推荐
- vs2015 相关
VS2015一共有3个版本,Visual Studio Community(社区版).Visual Studio Professional(专业版).Visual Studio Enterprise( ...
- gerrit 使用教程(一)
原文地址:https://www.jianshu.com/p/b77fd16894b6 1, Gerrit是什么? Gerrit实际上一个Git服务器,它为在其服务器上托管的Git仓库提供一系列权限控 ...
- Jmeter CSV Data Set Config参数化
在使用Jemeter做压力测试的时候,往往需要参数化用户名,密码以到达到多用户使用不同的用户名密码登录的目的.这个时候我们就可以使用CSV Data Set Config实现参数化登录: 首先通过Te ...
- JS参差不齐的数组
<html><head> <title>参差不齐的数组</title> <meta charset="utf-8"> & ...
- Mybatis—三剑客之generator使用方法
三剑客之generator主要用于自动生成POJO实体类 准备素材: mybatis-generator-core-1.3.2.jar mysql-connector-java-5.1.2 ...
- select,radio,checkbox兼容性
- kafka集群监控工具之三--kafka Offset Monitor
1.介绍 一般情况下,功能简单的kafka项目 使用运维命令+kafka Offset Monitor 就足够用了. 2.使用2.1 部署 github下载jar包 KafkaOffsetMonit ...
- 借助IDE到处Runnable JAR 的步骤
1. 选择项目,右键,export,选择Java目录下的Runnable JAR file , next 2. Lanch configuration 中选择启动类 3. Export destina ...
- 深入浅出JavaScript运行机制
一.引子 本文介绍JavaScript运行机制,这一部分比较抽象,我们先从一道面试题入手: console.log(1); setTimeout(function(){ console.log(3); ...
- Java系列笔记(0) - 目录和概述
笔者在开发过程中发现自己基础太薄弱,读书时除了系统学习了一下Java的基础语法和用法.一点简单的数据结构和设计模式之外,再无深入系统的学习,而工作中的学习也是东晃一枪西晃一枪,不够扎实和系统.想到一个 ...