ANSII 与Unicode,Utf8之间的转换
在项目开发中,我们难免会遇到各种问题,特别是字符直接的转换,这里列举字符直间转换的代码:
using namespace std;
wstring AnsiiToUnicode(const string& str) {
// 参数的长度
int strLen = str.length();
// 预算-缓冲区中宽字节的长度
int unicodeLen = MultiByteToWideChar(CP_ACP, , str.c_str(), -, nullptr, );
// 给指向缓冲区的指针变量分配内存
allocator<wchar_t> wc_t;
wchar_t *pUnicode = wc_t.allocate(sizeof(wchar_t)*(unicodeLen+),);
// 开始向缓冲区转换字节
MultiByteToWideChar(CP_ACP, , str.c_str(), -, pUnicode, unicodeLen);
wstring ret_str = pUnicode;
delete pUnicode;
return ret_str;
}
string UnicodeToAssii(const wstring& wstr) {
// 参数的长度
int wstrLen = wstr.length();
// 预算-缓冲区中多字节的长度
int ansiiLen = WideCharToMultiByte(CP_ACP, , wstr.c_str(), -, nullptr, ,nullptr,nullptr);
// 给指向缓冲区的指针变量分配内存
allocator<char> c_t;
char *pAssii = c_t.allocate(sizeof(char)*(ansiiLen + ), );
// 开始向缓冲区转换字节
WideCharToMultiByte(CP_ACP, , wstr.c_str(), -, pAssii, ansiiLen,nullptr,nullptr);
string ret_str = pAssii;
delete pAssii;
return ret_str;
}
wstring Utf8ToUnicode(const string& str) {
// 参数的长度
int strLen = str.length();
// 预算-缓冲区中宽字节的长度
int unicodeLen = MultiByteToWideChar(CP_UTF8, , str.c_str(), -, nullptr, );
// 给指向缓冲区的指针变量分配内存
allocator<wchar_t> wc_t;
wchar_t *pUnicode = wc_t.allocate(sizeof(wchar_t)*(unicodeLen + ), );
// 开始向缓冲区转换字节
MultiByteToWideChar(CP_UTF8, , str.c_str(), -, pUnicode, unicodeLen);
wstring ret_str = pUnicode;
delete pUnicode;
return ret_str;
}
string UnicodeToUtf8(const wstring& wstr) {
// 参数的长度
int wstrLen = wstr.length();
// 预算-缓冲区中多字节的长度
int ansiiLen = WideCharToMultiByte(CP_UTF8, , wstr.c_str(), -, nullptr, , nullptr, nullptr);
// 给指向缓冲区的指针变量分配内存
allocator<char> c_t;
char *pAssii = c_t.allocate(sizeof(char)*(ansiiLen + ), );
// 开始向缓冲区转换字节
WideCharToMultiByte(CP_UTF8, , wstr.c_str(), -, pAssii, ansiiLen, nullptr, nullptr);
string ret_str = pAssii;
delete pAssii;
return ret_str;
}
string AnsiiToUtf8(const string& str) {
return UnicodeToUtf8(AnsiiToUnicode(str));
}
源贴地址:http://tieba.baidu.com/p/4381031865
ANSII 与Unicode,Utf8之间的转换的更多相关文章
- utf8、ansii、unicode编码之间的转换
#include "stdafx.h"#include "windows.h"#include <iostream>#include <str ...
- 举例说明Unicode 和UTF-8之间的转换
1)写这篇博客的原因 首先我要感谢这篇博客,卡了很久,看完下面这篇博客终于明白Unicode怎么转换成UTF-8了. https://blog.csdn.net/qq_32252957/article ...
- Unicode和UTF-8之间的转换
转自:http://www.cnblogs.com/xdotnet/archive/2007/11/23/unicode_and_utf8.html#undefined 最近在用VC++开发一个小工具 ...
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
Unicode与UTF-8互转(C语言实现):http://blog.csdn.net/tge7618291/article/details/7599902 汉字 Unicode 编码范围:http: ...
- C/C++ GBK和UTF8之间的转换
{ 关于GBK和UTF-8之间的转换,很多初学者会很迷茫. 一般来说GBK和UTF-8是文字的编码方式,其对应的内码是不一样的,所以GBK和UTF-8的转换需要对内码进行一一映射,然后进行转换. 对于 ...
- C++中GB2312字符串和UTF-8之间的转换
在编程过程中需要对字符串进行不同的转换,特别是Gb2312和Utf-8直接的转换.在几个开源的魔兽私服中,很多都是老外开发的,而暴雪为了能 够兼容世界上的各个字符集也使用了UTF-8.在中国使用VS( ...
- ASCII Unicode UTF-8 之间的关系
转载请标明:https://i.cnblogs.com/EditPosts.aspx?opt=1 1. ASCII ASCII 只有127个字符,表示英文字母的大小写.数字和一些符号,但由于其他语言用 ...
- 【Java基础专题】编码与乱码(05)---GBK与UTF-8之间的转换
原文出自:http://www.blogjava.net/pengpenglin/archive/2010/02/22/313669.html 在很多论坛.网上经常有网友问" 为什么我使用 ...
- 编码与乱码(05)---GBK与UTF-8之间的转换--转载
原文地址:http://www.blogjava.net/pengpenglin/archive/2010/02/22/313669.html [GBK转UTF-8] 在很多论坛.网上经常有网友问“ ...
随机推荐
- 深入浅出Mybatis-sql自动生成
本文提供了一种自动生成sql语句的方法,它针对的对象是有主键或唯一索引的单表,提供的操作有增.删.改.查4种.理解本文和本文的提供的代码需要有java注解的知识,因为本文是基于注解生成sql的.本文适 ...
- Java代码到字节码——第一部分
Java代码到字节码——第一部分 原文地址 作者:James Bloom 译者:张坤 理解在Java虚拟机中Java代码如何别被编译成字节码并执行是非常重要的,因为这可以帮助你理解你的程序在运行时发生 ...
- Spring MVC中的HandlerMapping与HandlerAdapter
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- LaTeX入门教程
LaTeX(LATEX,音译"拉泰赫")是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用 ...
- Java定时器Timer简述
概述 主要用于Java线程里指定时间或周期运行任务.Timer是线程安全的,但不提供实时性(real-time)保证. 构造函数 Timer() 默认构造函数. Timer(boolean) 指定关联 ...
- Flexible 弹性盒子模型之CSS align-items 属性
实例 居中对齐弹性盒的各项 <div> 元素: div { display: flex; align-items:center; } 复制 效果预览 浏览器支持 表格中的数字表示支持该属性 ...
- [前端性能提升]--图片转化为base64
图片的 base64 编码就是可以将一副图片数据编码成一串字符串,使用该字符串代替图像地址 意义:网页上的每一个图片,都是需要消耗一个 http 请求下载而来的(所有才有了 csssprites 技术 ...
- Noip 2016
Day1 思路: 大致是 把一个环拆成链, 找某个人无非是向右找或向左找(即对当前点加或减) 若加上要移动的位置后坐标大于总人数, 就把当前坐标减去总人数, 若减去要移动的位置后坐标小于0, 就把当前 ...
- install OwnCloud9 on CentOS7
OwnCloud9下载地址: https://download.owncloud.org/community/owncloud-9.1.1.zip //安装配置数据库 #yum install mar ...
- Jquery 操作DropDownList 根据条件选中
$("#<%=DDLCounty.ClientID%> option").each(function () { if ($(this).text() == $(&quo ...