C++转换unicode utf-8 gb2312编码
windows开发环境下用VC++6.0 对unicode 、utf-8、 gb2312 三种编码格式之间的转换方法:
- #include <iostream>
- #include <string>
- #include <Windows.h>
- using namespace std;
- void unicodeToUTF8(const wstring &src, string& result)
- {
- int n = WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0 );
- result.resize(n);
- ::WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0 );
- }
- void unicodeToGB2312(const wstring& wstr , string& result)
- {
- int n = WideCharToMultiByte( CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0 );
- result.resize(n);
- ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)result.c_str(), n, 0, 0 );
- }
- void utf8ToUnicode(const string& src, wstring& result)
- {
- int n = MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, NULL, 0 );
- result.resize(n);
- ::MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());
- }
- void gb2312ToUnicode(const string& src, wstring& result)
- {
- int n = MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, NULL, 0 );
- result.resize(n);
- ::MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());
- }
- void printByte(string str)
- {
- int i=0;
- for (i=0; i<str.length(); i++)
- {
- printf("%X ",(unsigned char)str.at(i));
- }
- printf("\n");
- }
- void wprintByte(wstring str)
- {
- int i=0;
- for (i=0; i<str.length()*sizeof(wchar_t); i++)
- {
- printf("%X ",*((unsigned char*)str.c_str()+i));
- }
- printf("\n");
- }
- int main()
- {
- string strText = "AB汉字";
- string strUTF8;
- wstring wstrUnicode;
- string strGB2312;
- gb2312ToUnicode(strText, wstrUnicode);
- printf("Unicode=");
- wprintByte(wstrUnicode);
- unicodeToUTF8(wstrUnicode, strUTF8);
- printf("UTF-8 =");
- printByte(strUTF8);
- utf8ToUnicode(strUTF8,wstrUnicode);
- printf("Unicode=");
- wprintByte(wstrUnicode);
- unicodeToGB2312(wstrUnicode,strGB2312);
- printf("GB2312 =");
- printByte(strGB2312);
- return 0;
- }
这里用“AB汉字”这样一个字符串做测试,它的ASCII编码为41 42 BA BA D7 D6
输出结果:
C++转换unicode utf-8 gb2312编码的更多相关文章
- 字符集、字符编码、国际化、本地化简要总结(UNICODE/UTF/ASCII/GB2312/GBK/GB18030)
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 环境说明 普通的linux 和 普通的windows. ...
- C语言-字符编码转换:UTF与GB2312
依赖库libiconv,libiconv库的交叉编译不做描述,网上很多 #include <stdio.h> #include <stdlib.h> #include < ...
- 使用UTF8-CPP转换unicode编码 附录:UTF8和UTF16和UTF32和Unicode编码
本文用于解决如何用C++处理字符串的编码格式.本文采用的是成熟便捷的UTF8库来处理这个问题.首先是下载UTF8库,网址为:http://utfcpp.sourceforge.net/ 为了方便后续使 ...
- Unicode gbk gb2312 编码问题 [转载]
原文地址: http://www.cnblogs.com/csn0721/archive/2013/01/24/2875613.html HTML5 UTF-8 中文乱码 <!DOCTYPE ...
- Linux下将UTF8编码批量转换成GB2312编码的方法
Linux下将UTF8编码批量转换成GB2312编码的方法 在sqlplus中导入UTF8编码的sql脚本就会出现乱码错误,这时就需要将UTF8编码转换成GB2312编码,下面为大家介绍下在Linux ...
- ASP中Utf-8与Gb2312编码转换乱码问题的解决方法 页面编码声明
ASP程序在同一个站点中,如果有UTF-8编码的程序,又有GB2312编码的程序时,在浏览UTF-8编码的页面后,再浏览当前网站GB2312的页面,GB2312编码的页面就会出现乱码 出现这样的问题是 ...
- 趣谈unicode,ansi,utf-8,unicode big endian这些编码有什么区别(转载)
从头讲讲编码的故事.那么就让我们找个草堆坐下,先抽口烟,看看夜晚天空上的银河,然后想一想要从哪里开始讲起.嗯,也许这样开始比较好…… 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同 ...
- 做网站用UTF-8编码还是GB2312编码?
经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, WordPress程序是用的UTF-8,很多cms用的是GB2312. 经常我们打开外国网站的时 ...
- unicode ansi utf-8 unicode_big_endian编码的区别
随便说说字符集和编码 快下班时,爱问问题的小朋友Nico又问了一个问题: "sqlserver里面有char和nchar,那个n据说是指unicode的数据,这个是什么意思.&quo ...
- Unicode(UTF&UCS)深度历险
Unicode(UTF&UCS)深度历险 计算机网络诞生后,大家慢慢地发现一个问题:一个字节放不下一个字符了!因为需要交流,本地化的文字需要能够被支持. 最初的字符集使用7bit来存储字符,因 ...
随机推荐
- Easyui 加载树(easyui-tree)[dotnet]
前台 html: <ul class="easyui-tree" id="ul_Tree" data-options="fit:true,ani ...
- NodeJS V8 GC概览
[A tour of V8: Garbage Collection] http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection 基本是 ...
- Windows Phone 8.1 列表控件(1):基本
说到 List 控件,Windows Phone 8.1 上推荐使用的是 ListView 和 GridView. 而这两个控件实在太多东西可讲了,于是分成三篇来讲: (1)基本 (2)分组数据 (3 ...
- DQL_数据查询语言
2014年11月21日 21:43:53 DQL 基础查询-- 注意要点:1.用户友善的标题 ...
- ubuntu后台配置无线网络
一.静态配置: 1.编辑 /etc/network/interfaces: auto loiface lo inet loopback auto wlan0iface wlan0 inet stati ...
- 登堂入室——java流
——文章出自PeterYe,不得私自转载 我所知道的 java.io里面的[流],就仿佛太平洋里面的水一样,浩浩荡荡,横无际涯... -----2016/7/16--------公寓处记录------ ...
- Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢)
Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢) 1. 获取选中项: 获取选中项的Value值: $('select#sel option:selected').val() ...
- ArcGIS Server10.1授权文件
3dengine,101,ecp.arcgis.server,01-jan-2020,UTE784S3EY83ZJKN0085 3dserver,101,ecp.arcgis.server,01-ja ...
- sublime text2之js压缩-Js Minifier
一款基于Google Closure compiler压缩Js文件插件. 快捷键: Ctrl+Alt+M 当前文件内压缩Js代码(不推荐) Ctrl+Alt+Shift+M ...
- Cassandra1.2文档学习(12)—— hint机制
参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_about_ ...