Windows 下字节转换
Windows 下字节转换
#include <string>
#include <windows.h> class CharsConversion
{
public:
static bool MultiByte2UTF8( const std::string&, std::string& ); static bool UTF82MultiByte( const std::string&, std::string& ); static bool MultiByte2Unicode( const std::string&, std::wstring& ); static bool Unicode2MultiByte( const std::wstring&, std::string& ); static bool UTF82Unicode( const std::string&, std::wstring& ); static bool Unicode2UTF8( const std::wstring&, std::string& );
}; bool CharsConversion::MultiByte2UTF8( const std::string &strMB, std::string& strUTF8 )
{
//Note: If this cbMultiByte is –1, the string is assumed to be null terminated and the length is calculated automatically.
//Note: If cbMultiByte is strMB.size(), this function will not include the null terminated, it's an error.
//Note: Or, if cbMultiByte is strMB.size() + 1, this function will be right.
int nWCLen = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, NULL, ); wchar_t* pszWC = new (std::nothrow) wchar_t[nWCLen];
if ( nullptr == pszWC ) return false; //Note: The return value includes the NULL termination character.
int nRtn = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, pszWC, nWCLen );
if( nRtn != nWCLen ) { delete[] pszWC; return false; } // WindChar 2 UTF8
//Note: nWClen and -1 both ok.
int nUTF8Len = WideCharToMultiByte( CP_UTF8, , pszWC, nWCLen, NULL, , NULL, NULL );
if ( nUTF8Len <= ) { delete [] pszWC; return false; } strUTF8.resize( nUTF8Len );
nRtn = WideCharToMultiByte( CP_UTF8, , pszWC, nWCLen, &strUTF8[], nUTF8Len, NULL, NULL );
delete [] pszWC; if ( nRtn != nUTF8Len ) { strUTF8.clear(); return false; } return true;
} bool CharsConversion::UTF82MultiByte( const std::string &strUTF8, std::string &strMB )
{
//Note: cchWideChar must be -1 or strUTF8.size()+1.
int nWCLen = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, NULL, ); WCHAR* pszWC = new (std::nothrow) WCHAR[nWCLen];
if ( nullptr == pszWC ) return false; int nRtn = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, pszWC, nWCLen );
if ( nRtn != nWCLen ) { delete[] pszWC; return false; } //WideChar 2 MB
int nMBLen = WideCharToMultiByte( CP_ACP, , pszWC, nWCLen/*or -1*/, NULL, , NULL, NULL );
if ( nMBLen <= ) { delete [] pszWC; return false; } strMB.resize( nMBLen );
nRtn = WideCharToMultiByte( CP_ACP, , pszWC, nWCLen/*or -1*/, &strMB[], nMBLen, NULL, NULL );
delete [] pszWC; if ( nRtn != nMBLen ) { strMB.clear(); return false; } return true;
} bool CharsConversion::MultiByte2Unicode( const std::string &strMB, std::wstring &strUC )
{
//Note: cbMultiByte can be strMB.size() + 1 or -1.
int nUCLen = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, NULL, ); if ( nUCLen <= ) return false; strUC.resize( nUCLen ); int nRtn = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, &strUC[], nUCLen ); if ( nRtn != nUCLen ) { strUC.clear(); return false; } return true;
} bool CharsConversion::Unicode2MultiByte( const std::wstring &strUC, std::string &strMB )
{
//Note: cchWideChar must be -1 or strUC.size()+1.
int nMBLen = WideCharToMultiByte( CP_ACP, , strUC.c_str(), -, NULL, , NULL, NULL ); if ( nMBLen <= ) return false; strMB.resize( nMBLen ); int nRtn = WideCharToMultiByte( CP_ACP, , strUC.c_str(), -, &strMB[], nMBLen, NULL, NULL ); if( nRtn != nMBLen ) { strMB.clear(); return false; } return true;
} bool CharsConversion::UTF82Unicode( const std::string &strUTF8, std::wstring &strUC )
{
//Note: cbMultiByte can be strUTF8.size() + 1 or -1.
int nUCLen = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, NULL, ); if ( nUCLen <= ) return false; strUC.resize( nUCLen ); int nRtn = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, &strUC[], nUCLen ); if ( nRtn != nUCLen ) { strUC.clear(); return false; } return true;
} bool CharsConversion::Unicode2UTF8( const std::wstring &strUC, std::string &strUTF8 )
{
//Note: cchWideChar must be -1 or strUC.size()+1.
int nUTF8Len = WideCharToMultiByte( CP_UTF8, , strUC.c_str(), -, NULL, , NULL, NULL ); if ( nUTF8Len <= ) return false; strUTF8.resize( nUTF8Len ); int nRtn = WideCharToMultiByte( CP_UTF8, , strUC.c_str(), -, &strUTF8[], nUTF8Len, NULL, NULL ); if ( nRtn != nUTF8Len ) { strUTF8.clear(); return false; } return true;
}
Windows 下字节转换的更多相关文章
- windows下多字节和宽字节转换
先简单说下什么是多字节和宽字节. 多字节是指使用多个字节(1-3)表示一个字符.比如gbk使用英文占一个字节,中文占2个,这个就是多字节了.utf-8是使用1-3个字节表示字符.还有big5等等. 宽 ...
- 在windows下的QT编程中的_TCHAR与QString之间的转换
由于在windows下的QT编程中,如果涉及到使用微软的API,那么不可避免使用_TCHAR这些类型,因此在网上查了一下,其中一个老外的论坛有人给出了这个转换,因此在这里做一下笔记 : )#ifdef ...
- Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)
在windows下设置字节对齐大小的方式,目前我了解有三种: 1. 在编译程序时候的编译选项 /Zp[n],如 cl /Zp4 表示对齐大小是4字节: 2. 预处理命令 #pragma pack ...
- java实现windows下amr转换为mp3(可实现微信语音和qq语音转换)
最近做一个项目需要将微信的语音文件放在页面进行播放,查了好多资料发现,web页面直接播放并没有一个好的解决方案,于是就想到了先将amr文件转换成易于在页面播放的mp3文件,然后在进行播放,现在将amr ...
- windows下go编码转换问题
github上有两个package做编码转换,都是基于iconv,用到了cgo,在linux下没有问题,在windows下用,非常麻烦.采用mingw安装libiconv也不行,一直提示找不到libi ...
- C语言 windows下Ansi和UTF-8编码格式的转换
当我们使用MinGW-w64作为编译器在windows系统环境下进行C语言编程时,如果源代码文件(.c)保存格式为Ansi格式,则在打印汉字时不会出现乱码:反之,如果我们使用UTF-8格式保存,则会出 ...
- windows下mysql表名不自动转换小写配置
mysql5.6版本配置文件有两个 1.默认的配置在program files/MySQL/MySQL Server 5.6/my-default.ini 2.一个在programData/MySQL ...
- 原创 C++应用程序在Windows下的编译、链接:第三部分 静态链接(二)
3.5.2动态链接库的创建 3.5.2.1动态链接库的创建流程 动态链接库的创建流程如下图所示: 在系统设计阶段,主要的设计内容包括:类结构的设计以及功能类之间的关系,动态链接库的接口.在动态链接库中 ...
- 原创 C++应用程序在Windows下的编译、链接:第一部分 概述
本文是对C++应用程序在Windows下的编译.链接的深入理解和分析,文章的目录如下: 我们先看第一章概述部分. 1概述 1.1编译工具简介 cl.exe是windows平台下的编译器,link.ex ...
随机推荐
- java初学知识点
public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size.LARG ...
- 滚动RollUp、压缩
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 【Android】解析Json数据
Json数据:"{\"UserID\":\"Allen\",\"Dep\":IT,\"QQ\":\" ...
- 扩展XAF模型信息实现自定义功能
如何隐藏 web listview 的 编辑控制列如下图: 这列怎么让它隐藏? 感谢[少侠]XAF_杨东 提供解答!感谢XAF_小学生整理. A: 注册自定义接口IModelListViewExt ...
- cellery ImportError & AttributeError
一.zz的问题 celery 运行work要进入到 文件所在的文件夹下执行 二.AttributeError: 'Flask' object has no attribute 'user_option ...
- MFC编程入门之九(对话框:为控件添加消息处理函数)
这一节讲的主要内容是如何为控件添加消息处理函数. MFC为对话框和控件定义了诸多消息,我们对他们操作时会触发消息,这些消息最终由消息处理函数处理,比如我们点击按钮时就会产生BN_CLICKED消息,修 ...
- golang csv问题
go语言自带的有csv文件读取模块,看起来好像不错,今天玩玩,也算是系统学习go语言的一部分--^_^ 一.写csv文件 函数: func NewWriter(w io.Writer) *Writer ...
- b.Connector配置解析
前面讲解到Tomcat中使用Digester框架进行server.xml到javaBean对象的映射,这篇文章以Connector的SSL通道为例,来讲解Connector的属性是如何注入的. 先看一 ...
- 解决在国内更新android sdk时连不到服务器的问题
修改hosts文件 Windows下:打开C:\Windows\System32\drivers\etc\hosts Linux下:vi /etc/hosts 在文件尾加入如下两行: 74.125.2 ...
- Object.create()兼容实现方法
if (!Object.create) { Object.create = (function(){ function F(){} return function(o){ if (arguments. ...