在项目开发中,我们难免会遇到各种问题,特别是字符直接的转换,这里列举字符直间转换的代码:

  

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之间的转换的更多相关文章

  1. utf8、ansii、unicode编码之间的转换

    #include "stdafx.h"#include "windows.h"#include <iostream>#include <str ...

  2. 举例说明Unicode 和UTF-8之间的转换

    1)写这篇博客的原因 首先我要感谢这篇博客,卡了很久,看完下面这篇博客终于明白Unicode怎么转换成UTF-8了. https://blog.csdn.net/qq_32252957/article ...

  3. Unicode和UTF-8之间的转换

    转自:http://www.cnblogs.com/xdotnet/archive/2007/11/23/unicode_and_utf8.html#undefined 最近在用VC++开发一个小工具 ...

  4. 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)

    Unicode与UTF-8互转(C语言实现):http://blog.csdn.net/tge7618291/article/details/7599902 汉字 Unicode 编码范围:http: ...

  5. C/C++ GBK和UTF8之间的转换

    { 关于GBK和UTF-8之间的转换,很多初学者会很迷茫. 一般来说GBK和UTF-8是文字的编码方式,其对应的内码是不一样的,所以GBK和UTF-8的转换需要对内码进行一一映射,然后进行转换. 对于 ...

  6. C++中GB2312字符串和UTF-8之间的转换

    在编程过程中需要对字符串进行不同的转换,特别是Gb2312和Utf-8直接的转换.在几个开源的魔兽私服中,很多都是老外开发的,而暴雪为了能 够兼容世界上的各个字符集也使用了UTF-8.在中国使用VS( ...

  7. ASCII Unicode UTF-8 之间的关系

    转载请标明:https://i.cnblogs.com/EditPosts.aspx?opt=1 1. ASCII ASCII 只有127个字符,表示英文字母的大小写.数字和一些符号,但由于其他语言用 ...

  8. 【Java基础专题】编码与乱码(05)---GBK与UTF-8之间的转换

    原文出自:http://www.blogjava.net/pengpenglin/archive/2010/02/22/313669.html 在很多论坛.网上经常有网友问" 为什么我使用 ...

  9. 编码与乱码(05)---GBK与UTF-8之间的转换--转载

    原文地址:http://www.blogjava.net/pengpenglin/archive/2010/02/22/313669.html [GBK转UTF-8] 在很多论坛.网上经常有网友问“  ...

随机推荐

  1. hdu 4467 Graph

    P. T. Tigris is a student currently studying graph theory. One day, when he was studying hard, GS ap ...

  2. 12503 - Robot Instructions

      Robot Instructions  You have a robot standing on the origin of x axis. The robot will be given som ...

  3. jvm内存JVM学习笔记-引用(Reference)机制

    在写这篇文章之前,xxx已经写过了几篇关于改jvm内存主题的文章,想要了解的朋友可以去翻一下之前的文章 如果你还不了解JVM的基本概念和内存划分,请阅读JVM学习笔记-基础知识和JVM学习笔记-内存处 ...

  4. Java重写与重载之间的区别

    重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...

  5. 关于angularjs的$state.go()与ui-sref传参问题

    上次转发过关于angularjs回退的文章,回退用到的还是js的回退功能,直接用history.back();实现功能,当时顺便提了下$state.go()有关路由跳转. 那这回就全面解析下$stat ...

  6. 四、spark常用函数说明学习

    1.parallelize       并行集合,切片数.默认为这个程序所分配到的资源的cpu核的个数.       查看大小:rdd.partitions.size      sc.paraliel ...

  7. Prefix the choice with ! to persist it to bower.json ? Answer (问你选择哪个1,2,3.........)

    Unable to find a suitable version for underscore, please choose one by typing on e of the numbers be ...

  8. liunx服务器常见监控指标

    1. CPU Utilization 英文翻译就是CPU的利用率75%以上就比较高了(也有说法是80%或者更高).有的博客上说除了这个指标外,还要结合Load Average和Context Swit ...

  9. Lintcode解题报告

    1. Num.196 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 注意事项 可以改变序列中数的位置. 您在真实的面试中是否遇到过这个题 ...

  10. mysql5.7.17安装问题

    在根目录新建data文件夹和my.ini,把ini复制到bin目录下才可以