1、Unicode转UTF-8

 void CodeCovertTool::UNICODE_to_UTF8(const CString& unicodeString, std::string& str)
{//Unicode转UTF8
int stringLength = ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), NULL, , NULL, NULL); char* buffer = new char[stringLength + ];
::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), buffer, stringLength, NULL, NULL);
buffer[stringLength] = '\0'; str = buffer; delete[] buffer;
}
 std::string CodeCovertTool::UnicodeToUtf8(const wchar_t* buf)
{//Unicode转UTF8
int len = ::WideCharToMultiByte(CP_UTF8, , buf, -, NULL, , NULL, NULL);
if (len == ) return ""; std::vector<char> utf8(len);
::WideCharToMultiByte(CP_UTF8, , buf, -, &utf8[], len, NULL, NULL); return &utf8[];
}

2、UTF-8转Unicode

 std::wstring CodeCovertTool::Utf8ToUnicode(const char* buf)
{//UTF8转Unicode
int len = ::MultiByteToWideChar(CP_UTF8, , buf, -, NULL, );
if (len == ) return _T(""); std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_UTF8, , buf, -, &unicode[], len); return &unicode[];
}
 CString CodeCovertTool::Utf8ToUnicode(const std::string &utf8_str)
{//UTF8转Unicode
int len;
len = MultiByteToWideChar(CP_UTF8, , (LPCSTR)utf8_str.c_str(), -, NULL,);
WCHAR * wszUnicode = new WCHAR[len+];
memset(wszUnicode, , len * + );
MultiByteToWideChar(CP_UTF8, , (LPCSTR)utf8_str.c_str(), -, wszUnicode, len);
CString ss=wszUnicode;
delete wszUnicode;
return ss;
}

3、Ansi转Unicode

 std::wstring CodeCovertTool::AnsiToUnicode(const char* buf)
{//Ansi转Unicode
int len = ::MultiByteToWideChar(CP_ACP, , buf, -, NULL, );
if (len == ) return L""; std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_ACP, , buf, -, &unicode[], len); return &unicode[];
}

4、Unicode转Ansi

 std::string CodeCovertTool::UnicodeToAnsi(const wchar_t* buf)
{//Unicode转Ansi
int len = ::WideCharToMultiByte(CP_ACP, , buf, -, NULL, , NULL, NULL);
if (len == ) return ""; std::vector<char> utf8(len);
::WideCharToMultiByte(CP_ACP, , buf, -, &utf8[], len, NULL, NULL); return &utf8[];
}

UTF-8和Unicode互转的更多相关文章

  1. php 中文unicode 互转

    /** * $str 原始中文字符串 * $encoding 原始字符串的编码,默认GBK * $prefix 编码后的前缀,默认"&#" * $postfix 编码后的后 ...

  2. Unicode 互转

    // 转为unicode 编码 function encodeUnicode(str) { var res = []; ; i<str.length; i++ ) { res[i] = ( ) ...

  3. 中文和unicode互转

    public class Test { public static void main(String[] args) { String uname="欧阳红"; for (int ...

  4. Java 16进制、unicode互转

    package service; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CodeCh ...

  5. Java - 字符串和Unicode互转 - 解析小米pm.min.js

    小米JS地址: http://p.www.xiaomi.com/zt/20130313/huodong/pm.min.js 上面这个JS是小米抢手机页面的代码.和抢手机有直接关联.. 虽然我3次都没抢 ...

  6. java 中文与unicode互转

    public class FontUtil { public static void main(String[] args) { System.out.println(chinaToUnicode(& ...

  7. java字符串和unicode互转

    直接上代码 private static String decodeUnicode(String input) { if (null == input) return input; int len = ...

  8. Unicode、UTF-8 和 ISO8859-1到底有什么区别

    说明:本文转载于新浪博客,旨在方便知识总结.原文地址:http://blog.sina.com.cn/s/blog_673c81990100t1lc.html 本文主要包括以下几个方面:编码基本知识, ...

  9. 使用 WideCharToMultiByte Unicode 与 UTF-8互转

    1.简述 最近在发送网络请求时遇到了中文字符乱码的问题,在代码中调试字符正常,用抓包工具抓的包中文字符显示正常,就是发送到服务器就显示乱码了,那就要将客户端和服务器设置统一的编码(UTF-8),而我们 ...

随机推荐

  1. Zabbix 3.0安装

    Server 1. rpm安装zabbix 3.0最新的epel源 rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-re ...

  2. bb=Discuz与 Discuz! X ,Discuz!NT区别

    没加x的,仅仅是单独的论坛. 加了x的,模块加了很多了,门户,家园,排行榜,群组,都是Discuz! X上的,而Discuz!上没有,所以说Discuz! X更加适用于建设门户网 Discuz! X ...

  3. 学习某些API的方法

    学习某些 API 的方法 这里的 API 可能是某个系统平台,开发包,开发平台,开发工具等等,因为任何系统和技术方法提供给开发者的打包方式都是一系列 API . 无论你有在哪一层级开发,从硬件驱动到系 ...

  4. iOS开发中的单元测试(三)——URLManager中的测试用例解析

    本文转载至 http://www.cocoachina.com/cms/plus/view.php?aid=8088   此前,我们在<iOS开发中的单元测试(一)&(二)>中介绍 ...

  5. 微信小程序开发:学习笔记[9]——本地数据缓存

    微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...

  6. MYSQL进阶学习笔记十六:MySQL 监控!(视频序号:进阶_35)

    知识点十七:MySQL监控(35) 一.为什么使用MySQL监控 随着软件后期的不断升级,myssql的服务器数量越来越多,软硬件故障的发生概率也越来越高.这个时候就需要一套监控系统,当主机发生异常时 ...

  7. Myeclipse+TestNG白盒测试环境搭建

    myeclipse基础环境搭建这里不讲了,我用的是myeclipse2013,网上有破解方法http://jingyan.baidu.com/article/aa6a2c14d0c9700d4c19c ...

  8. 最基本PSO算法的C++实现

    按照James Kennedy & Russell Eberhart (1995)的版本,算法过程如下: [x*] = PSO() P = Particle_Initialization(); ...

  9. 【C/C++】计算两个整数的最大公约数和最小公倍数

    算法一 任何>1的整数都可以写成一个或多个素数因子乘积的形式,且素数乘积因子以非递减序出现. 则整数x,y可以分别标记为:x=p1x1p2x2...pmxm y=p1y1p2y2...pmym ...

  10. Relative atomic mass

    Relative atomic mass Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...