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. 3. Swift 数组|字典|集合

    在OC中数组,字典,集合有自己的表示方法,分别是Array,Dictionary,Set 与 String 都属于数值类型变量,他们都属于结构体. 使用简介灵活多变,个人感觉可读性变差了很多,用起来由 ...

  2. 使用redis-dump进行Redis数据库合并

    前言 最近处理数据时,涉及到跨服务器访问的问题,我有两个Redis服务器分别在不同的机器上,给数据维护带来了诸多不便,于是便研究了下如何将两个Redis中的数据合并到一处. 从网站搜了一些工具,找到了 ...

  3. GridFS图片

    -----------2016-5-9 18:58:56-- source:GridFS实现图片的存取

  4. js中面向对象

    1.对象的表示方法,以下是对象的两种方法:第二种方法是使用函数构造器来创建一个对象. 2.对象的一种表达方式,这种方式更像Java中对象的创建,就是用一个new来创建一个对象实例.面向对象的封装.样式 ...

  5. c#遍历并判断实体或类的成员属性

    c#的Attribute有些类似java中的annotation,可以方便地在类成员中做修饰/限制作用. Demo: class ss { public stat BsonDocument Itera ...

  6. linux笔记:文件系统管理-分区、文件系统以及文件系统常用命令

    linux分区类型: linux文件系统: df(查看文件系统容量和占用): du(统计文件或目录大小): 查询和自动挂载: 挂载命令格式: 挂载光盘: 卸载光盘: 挂载U盘:

  7. Yii1.1.16的安装(windows)

    下载yii1.1.16 http://pan.baidu.com/s/1mgyAOnI 解压后放到D盘,并重新命名为yii1.1.16,我是直接放在开发的目录下面 按"Window + R& ...

  8. 最短路径——Floyd算法

    如何求一张图中任意两顶点之间的最短路径长度,这里写一种最简单的算法——Floyd算法: #include<stdio.h> #define inf 9999 int main() { ][ ...

  9. 媒体查询判断ipad与iPhone各版本i

    /* 判断ipad */ @media only screen and (min-device-width : 768px) and (max-device-width : 1024px){ /* s ...

  10. session如何保存在专门的StateServer服务器中

    session保存在专门的StateServer中,该种方式,性能损失比sql略好.比inproc据说有10%-15%的性能损失.怎么使用StateServer 服务器呢? 1.初始化StateSer ...