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 下字节转换的更多相关文章

  1. windows下多字节和宽字节转换

    先简单说下什么是多字节和宽字节. 多字节是指使用多个字节(1-3)表示一个字符.比如gbk使用英文占一个字节,中文占2个,这个就是多字节了.utf-8是使用1-3个字节表示字符.还有big5等等. 宽 ...

  2. 在windows下的QT编程中的_TCHAR与QString之间的转换

    由于在windows下的QT编程中,如果涉及到使用微软的API,那么不可避免使用_TCHAR这些类型,因此在网上查了一下,其中一个老外的论坛有人给出了这个转换,因此在这里做一下笔记 : )#ifdef ...

  3. Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)

    在windows下设置字节对齐大小的方式,目前我了解有三种: 1. 在编译程序时候的编译选项  /Zp[n],如 cl /Zp4 表示对齐大小是4字节: 2. 预处理命令   #pragma pack ...

  4. java实现windows下amr转换为mp3(可实现微信语音和qq语音转换)

    最近做一个项目需要将微信的语音文件放在页面进行播放,查了好多资料发现,web页面直接播放并没有一个好的解决方案,于是就想到了先将amr文件转换成易于在页面播放的mp3文件,然后在进行播放,现在将amr ...

  5. windows下go编码转换问题

    github上有两个package做编码转换,都是基于iconv,用到了cgo,在linux下没有问题,在windows下用,非常麻烦.采用mingw安装libiconv也不行,一直提示找不到libi ...

  6. C语言 windows下Ansi和UTF-8编码格式的转换

    当我们使用MinGW-w64作为编译器在windows系统环境下进行C语言编程时,如果源代码文件(.c)保存格式为Ansi格式,则在打印汉字时不会出现乱码:反之,如果我们使用UTF-8格式保存,则会出 ...

  7. windows下mysql表名不自动转换小写配置

    mysql5.6版本配置文件有两个 1.默认的配置在program files/MySQL/MySQL Server 5.6/my-default.ini 2.一个在programData/MySQL ...

  8. 原创 C++应用程序在Windows下的编译、链接:第三部分 静态链接(二)

    3.5.2动态链接库的创建 3.5.2.1动态链接库的创建流程 动态链接库的创建流程如下图所示: 在系统设计阶段,主要的设计内容包括:类结构的设计以及功能类之间的关系,动态链接库的接口.在动态链接库中 ...

  9. 原创 C++应用程序在Windows下的编译、链接:第一部分 概述

    本文是对C++应用程序在Windows下的编译.链接的深入理解和分析,文章的目录如下: 我们先看第一章概述部分. 1概述 1.1编译工具简介 cl.exe是windows平台下的编译器,link.ex ...

随机推荐

  1. [poj2828] Buy Tickets (线段树)

    线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...

  2. 动态创建的DOM元素进行事件绑定

    http://files.cnblogs.com/files/xsmhero/jquery.livequery.js <script type="text/javascript&quo ...

  3. 为ssh增加选项

    在使用ssh的时候,可以看到ssh有很多功能,什么-o , -e等等.如下图 需求,想要给ssh增加一个参数的功能.比如说我现在的需求就是执行ssh的时候可以增加一个选项,给我每次ssh的操作搭一个标 ...

  4. 【OOAD】设计模式概述

    模式的诞生与定义 模式起源于建筑业而非软件业模式(Pattern)之父——美国加利佛尼亚大学环境结构中心研究所所长Christopher Alexander博士<A Pattern Langua ...

  5. JSPServlet精华笔记

    一.     JSP (Java Server Pages) JSP是指: ▶    在HTML中嵌入Java脚本代码 ▶    由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本代码 ▶    ...

  6. Android 控件知识点,

    一.Android控件具有visibility属性,可以取三个值:visible(默认值)可见,invisible(不可见,但仍然占据原有的位置和大小,可以看做是变得透明了),gone(空间不仅不可见 ...

  7. iOS开发 沙盒路径和使用

    1.模拟器沙盒目录文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library.因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件: ...

  8. 使用 GPG 对数据进行加密解密签名

    一:使用 GPG 对数据进行加密解密签名 基本的工具使用 1. GPG 是GNUPG 免费开源的gpg加密工具,和同pgp兼容,pgp收费. 2. 在mac上使用https://gpgtools.or ...

  9. ios批量打包

    最近我们接到了新的需求,需要打出类似xx001-xx100共100个这样的ipa渠道包,不需要签名.(这批ipa包后续会用企业证书签名,不会影响AppStore的) 这些包所有的功能.内容都是一样的, ...

  10. AFN实现多图片上传的方法

    [[AFAppDotNetAPIClient sharedClient] POST:path parameters:dic constructingBodyWithBlock:^(id<AFMu ...