mfc字符转码
std::wstring UTF8ToUnicode(const std::string& utf8string)
{
int widesize = ::MultiByteToWideChar(CP_UTF8, , utf8string.c_str(), -, NULL, );
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == )
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = ::MultiByteToWideChar(CP_UTF8, , utf8string.c_str(), -, &resultstring[], widesize);
if (convresult != widesize)
{
throw std::exception("La falla!");
}
return std::wstring(&resultstring[]);
}
std::string WideByteToAcsi(std::wstring& wstrcode)
{
int asciisize = ::WideCharToMultiByte(CP_OEMCP, , wstrcode.c_str(), -, NULL, , NULL, NULL);
if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (asciisize == )
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(asciisize);
int convresult = ::WideCharToMultiByte(CP_OEMCP, , wstrcode.c_str(), -, &resultstring[], asciisize, NULL, NULL);
if (convresult != asciisize)
{
throw std::exception("La falla!");
}
return std::string(&resultstring[]);
}
std::string UTF8ToASCII(std::string& strUtf8Code)
{
std::string strRet("");
//先把 utf8 转为 unicode
std::wstring wstr = UTF8ToUnicode(strUtf8Code);
//最后把 unicode 转为 ascii
strRet = WideByteToAcsi(wstr);
return strRet;
}
std::string ASCIIToUTF8(std::string& strASCIICode)
{
std::string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, , strASCIICode.c_str(), -, NULL, );
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, , strASCIICode.c_str(), -, str1, n);
n = WideCharToMultiByte(CP_UTF8, , str1, -, NULL, , NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, , str1, -, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete[]str1;
str1 = NULL;
delete[]str2;
str2 = NULL;
return strOutUTF8;
}
//!!为了减少字符串的拷贝,改为使用shared_array,性能升级
#include <boost/shared_array.hpp>
#define CHAR_EMPTY_ARR_PTR boost::shared_array<char>(new char[1]{'\0'})
#define WCHAR_EMPTY_ARR_PTR boost::shared_array<wchar_t>(new wchar_t[1]{L'\0'})
boost::shared_array<char> UTF8ToASCII(std::string& strUtf8Code)
{
return std::move(UTF8ToASCII(strUtf8Code.c_str()));
}
boost::shared_array<char> UTF8ToASCII(const char* ch, int nLen /*= -1*/)
{
//先把 utf8 转为 unicode
auto wstr = UTF8ToUnicode(ch, nLen);
//最后把 unicode 转为 ascii
return std::move(WideByteToAcsi(wstr.get()));
}
boost::shared_array<char> ASCIIToUTF8(std::string& strASCIICode)
{
return std::move(ASCIIToUTF8(strASCIICode.c_str()));
}
boost::shared_array<char> ASCIIToUTF8(const char* ch, int nLen/* = -1*/)
{
int n = MultiByteToWideChar(CP_ACP, , ch, nLen, NULL, );
if ( == n)
{
return CHAR_EMPTY_ARR_PTR;
}
boost::shared_array<wchar_t> ptrWtArr(new wchar_t[n]{ L'\0' });
if ( == MultiByteToWideChar(CP_ACP, , ch, nLen, ptrWtArr.get(), n))
{
return CHAR_EMPTY_ARR_PTR;
}
n = WideCharToMultiByte(CP_UTF8, , ptrWtArr.get(), -, NULL, , NULL, NULL);
if ( == n)
{
return CHAR_EMPTY_ARR_PTR;
}
boost::shared_array<char> ptrArr(new char[n]{ '\0' });
if ( == WideCharToMultiByte(CP_UTF8, , ptrWtArr.get(), -, ptrArr.get(), n, NULL, NULL))
{
return CHAR_EMPTY_ARR_PTR;
}
return std::move(ptrArr);
}
boost::shared_array<wchar_t> UTF8ToUnicode(const char* ch, int nLen /*= -1*/)
{
int widesize = ::MultiByteToWideChar(CP_UTF8, , ch, nLen, NULL, );
if ( == widesize)
{
return WCHAR_EMPTY_ARR_PTR;
}
boost::shared_array<wchar_t> ptrWtArr(new wchar_t[widesize]{ L'\0' });
int convresult = ::MultiByteToWideChar(CP_UTF8, , ch, nLen, ptrWtArr.get(), widesize);
if ( == convresult)
{
return WCHAR_EMPTY_ARR_PTR;
}
return std::move(ptrWtArr);
}
boost::shared_array<char> DPC::WideByteToAcsi(std::wstring& wstrcode)
{
return std::move(WideByteToAcsi(wstrcode.c_str()));
}
boost::shared_array<char> WideByteToAcsi(const wchar_t* wch, int nLen /*= -1*/)
{
int asciisize = ::WideCharToMultiByte(CP_OEMCP, , wch, nLen, NULL, , NULL, NULL);
if ( == asciisize)
{
return CHAR_EMPTY_ARR_PTR;
}
boost::shared_array<char> ptrArr(new char[asciisize + ]{'\0'});
int convresult = ::WideCharToMultiByte(CP_OEMCP, , wch, nLen, ptrArr.get(), asciisize, NULL, NULL);
if ( == convresult)
{
return CHAR_EMPTY_ARR_PTR;
}
return std::move(ptrArr);
}
mfc字符转码的更多相关文章
- native2ascii.exe 字符转码与反转码
本人最近在做OAF的二次开发,在看别人写的代码时,发现总有类似这样的语句:”\u65e0\u6548\u7684GP\u9879\u76ee\u7f16\u53f7“,这些语句其实是用Java_hom ...
- C#编程总结(十)字符转码
C#编程总结(十)字符转码 为了适应某种特殊需要,字符需要根据规则进行转码,便于传输.展现以及其他操作等. 看看下面的转码,就知道他的用处了. 1.字符串转码 根据原编码格式与目标编码格式,完成转换. ...
- 【jquery】字符ascii码转换函数
js 字符ascii码转换函数 字符转ascii码:用charCodeAt();ascii码砖字符:用fromCharCode(); 看一个小例子 <script> str="A ...
- 关于htmlspecialchars实体字符转码的问题
php对post过来的数据进行实体字符转码,我的页面编码是gb2312,刚开始是这样: $post = htmlspecialchars ( $post); 取到的$post值为空,但是有时候是好的( ...
- python:字符串转换成字节的三种方式及字符转码问题
str='zifuchuang' 第一种 b'zifuchuang'第二种bytes('zifuchuang',encoding='utf-8')第三种('zifuchuang').encode('u ...
- Go url编码和字符转码
类似php中的urlencode 和htmlspecialchars: package main import ( "fmt" "html" "net ...
- String中文字符转码
如何使用String构造方法和String.getBytes()做好中文字符转码 @Test public void test() { String testStr = "中"; ...
- C#编程总结 字符转码
为了适应某种特殊需要,字符需要根据规则进行转码,便于传输.展现以及其他操作等. 看看下面的转码,就知道他的用处了. 1.字符串转码 根据原编码格式与目标编码格式,完成转换.不过可能出现乱码哦.上一章已 ...
- MFC基础类源码CPP实现文件
WinMain.CPP---->AfxWinMain() //近似可认为是WinMain()函数的入口 VIEWCORE.CPP---->CView DOCCORE.CPP----> ...
随机推荐
- cmd中添加目录md
md 创建目录. MKDIR [drive:]pathMD [drive:]path 如果命令扩展被启用,MKDIR 会如下改变: 如果需要,MKDIR 会在路径中创建中级目录.例如: 假设 \a 不 ...
- Spring Boot(一) Hello World
一.Spring Boot之我见 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从 ...
- 03爬虫-requests模块基础(1)
requests模块基础 什么是requests模块 requests模块是python中原生基于网络模拟浏览器发送请求模块.功能强大,用法简洁高效. 为什么要是用requests模块 用以前的url ...
- vue-router路由元信息及keep-alive组件级缓存
路由元信息?(黑人问号脸???)是不是这么官方的解释很多人都会一脸懵?那么我们说meta,是不是很多人恍然大悟,因为在项目中用到或者看到过呢? 是的,路由元信息就是我们定义路由时配置的meta字段:那 ...
- 基于操作系统的Linux网络参数的配置
一.实验目的 1.掌握Linux下网络参数的查看方法并理解网络参数的含义. 2.掌握Linux下网络参数的配置 二.实验内容 1.查看当前网络配置的参数. 2.在Linux主机中将网络参数按以下要求设 ...
- SSH Config 管理多主机
使用 一般我们使用ssh连接远程主机的时候,使用命令是: ssh root@ip ssh –i [identity-file] -p [port] user@hostname 但是如果ip地址过多,其 ...
- Flask基础(13)-->Flask扩展Flask-Script
Flask基础(12)-->Flask扩展Flask-Script # 前提是安装了Flask-Script # 联网运行 pip install flask-script from flask ...
- linux常用开发命令总结
linux常用命令 文件操作命令 1. cd 目录名/目录名 切换目录 cd .. 切换到上一级目录 (change dictionary) Ctrl+C强制退出命令行,回到上一级 2.ls ...
- Java基础学习笔记(二) - 面向对象基础
面向对象 一.面向对象概述 面向对象思想就是在计算机程序设计过程中,参照现实事物,将事物的属性特征.行为特征抽象出来,描述成计算机时间的设计思想.面向对象思想区别于面向过程思想,强调的是通过调用对象的 ...
- Oracle 存储过程判断语句正确写法和时间查询方法
判断语句:if 条件 then if 条件 then ************; elsif 条件 then ************; elsif 条件 then ***** ...