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来存储字符,因 ...
随机推荐
- linux/centos下安装nginx(rpm安装和源码安装)详细步骤
Centos下安装nginx rpm包 ...
- Unity User Group 北京站图文报道:《Unity3D VR游戏与应用开发》
很高兴,能有机会回报Unity技术社区:我和雨松MOMO担任UUG北京站的负责人, 组织Unity技术交流和分享活动. 本次北京UUG活动场地–微软大厦 成功的UUG离不开默默无闻的付出:提前2小时到 ...
- 第十三篇、Swift_Nav自定义返回按钮后或者隐藏导航栏,Pop返回手势失效的解决方法 Pop全局返回添加的方法
边缘的pop返回手势: override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.purple ...
- IOS 异步GET方法请求
1.添加协议NSURLConnectionDelegate 2.引入头文件“NSString+URLEncoding”,用来处理URL进行编码. 3.引入头文件“NSNumber+Message”,用 ...
- 简介spring中MethodReplacer的用法
欢迎转载交流:个人博客地址http://www.cnblogs.com/shizhongtao/p/3468713.html org.springframework.beans.factory.sup ...
- android app widget 创建调用周期
1 ?Android widget 大小问题 2 ?RemoteViewService Android开发历程_15(AppWidget的使用) Appwidget就是手机应用中常常放在桌面(即hom ...
- Maven Dependency Scope用法
原帖地址:http://uule.iteye.com/blog/2087485 官方API描述 Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个 ...
- AVA取整以及四舍五入
AVA取整以及四舍五入 import java.math.BigDecimal; public class Test { public static void main(String[] args) ...
- Asp.Net Web API VS Asp.Net MVC
http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...
- undefined local variable or method ‘xxx’ for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xbc88d6c>错误解决方案
官方文档如下: https://github.com/rspec/rspec-rails/blob/master/Capybara.md Capybara 2.0 To use Capybara 2. ...