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来存储字符,因 ...
随机推荐
- Android之进度条1
第一种方法(比较简单): package com.example.dialogdemo; import java.util.Random; import android.app.Activity; i ...
- C盘空间不足,释放C盘空间
最近电脑总是特别卡,后来发现C盘空间严重不足,只剩下几十兆,以前最严重的时候是剩下0kb可以,怎一个惨字了得... 我所知道的C盘空间不足会导致的几个主要问题有: 1)拷贝大文件会失败.因为拷贝和剪切 ...
- Cocos2d-x内置粒子系统
从类图中我们可以看到,Cocos2d-x中有内置的11粒子,这些粒子的属性都是预先定义好的,我们也可以在程序代码中单独修改某些属性,我们在上一节的实例中都已经实现了这些属性的设置. 内置粒子系统 内置 ...
- 快速搭建MongoDB分布式集群
目录Outline 1. prerequisites 2. steps to follow3. configuring the cluster4. a little test to see 1. Pr ...
- lex&yacc4
yacc: we cannt use the $$ value dirictly. we need get it irrotly;
- 转载:邮箱正则表达式Comparing E-mail Address Validating Regular Expressions
Comparing E-mail Address Validating Regular Expressions Updated: 2/3/2012 Summary This page compares ...
- 一种C# TCP异步编程中遇到的问题
最近在维护公司的一个socket服务端工具,该工具主要是提供两个socket server服务,对两端连接的程序进行数据的透明转发. 程序运行期间,遇到一个问题,程序的一端是GPRS设备,众所周知,G ...
- android eclipse集成环境
Android开发工具(ADT)是一个插件为Eclipse IDE,它的目的是给你一个强大的,集成的环境来构建Android应用程序. ADT扩展了Eclipse的功能使用Android SDK工具, ...
- 关于maven参数过滤
一.maven通过设置过滤器,可以使maven在编译打包时实现参数过滤的功能(详细配置说明略) <filters> <filter>../antx.properties< ...
- 概念:RPG与RPGLE的区别
RPG是OPM编程模式,即RPG编程的代码不能编译成*MODULE:编译只能直接生成一个程序,*PGM. RPGLE是ILE编程模式.OS/400环境下,ILE是集成开发环境.在ILE环境下,所 ...