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 ...
随机推荐
- python 字符串 转 dict
比直接eval更好的方法>>>import ast >>>ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'k ...
- [问题2014A02] 解答二(求和法+拆分法,由张诚纯同学提供)
[问题2014A02] 解答二(求和法+拆分法,由张诚纯同学提供) 将行列式 \(|A|\) 的第二列,\(\cdots\),第 \(n\) 列全部加到第一列,可得 \[ |A|=\begin{vma ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs序
D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- (原创)Windows和Linux间共享文件
方法一: Windows创建目录sharedir,修改共享属性,对everyone开放读写权限. Linux下运行命令:# mount -t cifs //192.168.8.55/sharedir ...
- 转 一个典型的 C++ 程序员成长经历:
1. 完整的学一遍 C++ 所有语言特性,典型书籍 "The C++ Programming Language" Part1, Part2, "C++ Primer&q ...
- [SoapUI] JDBC 请求连接SQL Sever,MySQL
MySQLDriver: com.mysql.jdbc.DriverConnection string: jdbc:mysql://localhost:3306/xxx?user=xxx&pa ...
- MAC air 安装redis-3.0.6
redis版本: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Menlo; color: #c33720; background-colo ...
- linux笔记:文件系统管理-fdisk分区
fdisk命令分区过程: 1.添加新硬盘 2.查看新硬盘: fdisk -l 3.使用fdisk命令分区: fdisk 硬盘设备文件名(如:fdisk /dev/sdb) fdisk交互指令说明: 4 ...
- Port Hacking
Port information21 ftp 主要看是否支持匿名,也可以跑弱口令22 ssh23 telnet79 Finger80 web 常见web漏洞以及是否为一些管理后台111 rpcinfo ...