UTF-8和Unicode互转
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互转的更多相关文章
- php 中文unicode 互转
/** * $str 原始中文字符串 * $encoding 原始字符串的编码,默认GBK * $prefix 编码后的前缀,默认"&#" * $postfix 编码后的后 ...
- Unicode 互转
// 转为unicode 编码 function encodeUnicode(str) { var res = []; ; i<str.length; i++ ) { res[i] = ( ) ...
- 中文和unicode互转
public class Test { public static void main(String[] args) { String uname="欧阳红"; for (int ...
- Java 16进制、unicode互转
package service; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CodeCh ...
- Java - 字符串和Unicode互转 - 解析小米pm.min.js
小米JS地址: http://p.www.xiaomi.com/zt/20130313/huodong/pm.min.js 上面这个JS是小米抢手机页面的代码.和抢手机有直接关联.. 虽然我3次都没抢 ...
- java 中文与unicode互转
public class FontUtil { public static void main(String[] args) { System.out.println(chinaToUnicode(& ...
- java字符串和unicode互转
直接上代码 private static String decodeUnicode(String input) { if (null == input) return input; int len = ...
- Unicode、UTF-8 和 ISO8859-1到底有什么区别
说明:本文转载于新浪博客,旨在方便知识总结.原文地址:http://blog.sina.com.cn/s/blog_673c81990100t1lc.html 本文主要包括以下几个方面:编码基本知识, ...
- 使用 WideCharToMultiByte Unicode 与 UTF-8互转
1.简述 最近在发送网络请求时遇到了中文字符乱码的问题,在代码中调试字符正常,用抓包工具抓的包中文字符显示正常,就是发送到服务器就显示乱码了,那就要将客户端和服务器设置统一的编码(UTF-8),而我们 ...
随机推荐
- ORA-00600: internal error code, arguments: [2662], [0], [1106971], [0], [1107731], [12583040]
今天是2014-06-06,在进行数据库恢复的时候出现了一个久违的ora-600 [2662]错误.特整理例如以下: 问题描写叙述: system及数据文件误删,採用恢复数据文件的方式将数据库恢复到開 ...
- Day20 Java Socket使用
Java中Socket的使用 client端 package org.tizen.test; import java.io.IOException; import java.io.OutputStre ...
- 在VS2015中增加JQuery引用及智能提示
打开VS2015,从"工具"菜单选择NuGet选项,搜索Jquery,并点击安装. 可以看到解决方案的scripts增加了对应文件的引用 在HTML文件中可以直接引用j ...
- 【BZOJ3728】PA2014Final Zarowki 贪心
[BZOJ3728]PA2014Final Zarowki Description 有n个房间和n盏灯,你需要在每个房间里放入一盏灯.每盏灯都有一定功率,每间房间都需要不少于一定功率的灯泡才可以完全照 ...
- 【BZOJ4956】lydsy七月月赛 I 乱搞
[BZOJ4956]lydsy七月月赛 I 题面 题解:傻题,Floyd传递闭包即可~ #include <cstdio> #include <cstring> #includ ...
- EasyIPCamera实现的桌面采集直播用于课堂、会议、展销同屏等应用
本文转自博客:http://blog.csdn.net/jinlong0603/article/details/56664233 Android同屏直播 在Android上除了获取摄像头数据为Easy ...
- 我们将要建立的EasyDarwin开源社区
从12年12月我开始建立EasyDarwin开源项目,已经三年多的时间了,从开始最简单的一个开源流媒体服务器项目,如今已经发展成为目前国内最大的一个流媒体开源社区,截至目前已经有十几个项目在Githu ...
- Go Web(一)
Beego环境搭建和bee工具安装使:http://blog.csdn.net/qq_534019165/article/details/48288133 Go语言beego框架环境搭建:http:/ ...
- asp.net 列表控件
web空间类都被放置在System.Web.UI.WebControls命名空间下1.ListBox ListBox控件用于创建多选的下拉列表,而可选项是通过ListItem元素来定义的.示例代码如 ...
- 电脑插入U盘后显示CD驱动器,如何还原为正常U盘?
以前用电脑店制作U盘启动工具,结果U盘的一小部分变成了CD驱动器, 无论怎么格式化都无法将U盘还原,这几天心血来潮,看了一些恢复U盘的教程, 打算写一篇博客讲解一下还原过程(本人亲测) 1.下载Chi ...