unicode字符和多字节字符的相互转换接口
作者:朱金灿
来源:http://blog.csdn.net/clever101
发现开源代码的可利用资源真多,从sqlite3的源码中抠出了几个字符转换接口,稍微改造下了发现还挺好用的。下面是实现代码:
- /*
- ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
- **
- ** Space to hold the returned string is obtained from malloc.
- */
- static WCHAR *utf8ToUnicode(const char *zFilename){
- int nChar;
- WCHAR *zWideFilename;
- nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
- zWideFilename = static_cast<WCHAR *>(malloc(nChar*sizeof(zWideFilename[0])));
- if( zWideFilename==0 ){
- return 0;
- }
- nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
- if( nChar==0 ){
- free(zWideFilename);
- zWideFilename = 0;
- }
- return zWideFilename;
- }
- /*
- ** Convert microsoft unicode to UTF-8. Space to hold the returned string is
- ** obtained from malloc().
- */
- static char *unicodeToUtf8(const WCHAR *zWideFilename){
- int nByte;
- char *zFilename;
- nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
- zFilename = static_cast<char*>(malloc( nByte ));
- if( zFilename==0 ){
- return 0;
- }
- nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
- 0, 0);
- if( nByte == 0 )
- {
- free(zFilename);
- zFilename = 0;
- }
- return zFilename;
- }
- /*
- ** Convert an ansi string to microsoft unicode, based on the
- ** current codepage settings for file apis.
- **
- ** Space to hold the returned string is obtained
- ** from malloc.
- */
- static WCHAR *mbcsToUnicode(const char *zFilename){
- int nByte;
- WCHAR *zMbcsFilename;
- int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
- nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
- zMbcsFilename = static_cast<WCHAR*>(malloc( nByte*sizeof(zMbcsFilename[0])));
- if( zMbcsFilename==0 ){
- return 0;
- }
- nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
- if( nByte==0 )
- {
- free(zMbcsFilename);
- zMbcsFilename = 0;
- }
- return zMbcsFilename;
- }
- /*
- ** Convert microsoft unicode to multibyte character string, based on the
- ** user's Ansi codepage.
- **
- ** Space to hold the returned string is obtained from
- ** malloc().
- */
- static char* unicodeToMbcs(const WCHAR* zWideFilename){
- int nByte;
- char *zFilename;
- int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
- nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
- zFilename = static_cast<char*>(malloc(nByte ));
- if( zFilename==0 ){
- return 0;
- }
- nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
- 0, 0);
- if( nByte == 0 ){
- free(zFilename);
- zFilename = 0;
- }
- return zFilename;
- }
- /*
- ** Convert multibyte character string to UTF-8. Space to hold the
- ** returned string is obtained from malloc().
- */
- static char* mbcsToUtf8(const char *zFilename){
- char *zFilenameUtf8;
- WCHAR *zTmpWide;
- zTmpWide = mbcsToUnicode(zFilename);
- if( zTmpWide==0 ){
- return 0;
- }
- zFilenameUtf8 = unicodeToUtf8(zTmpWide);
- free(zTmpWide);
- return zFilenameUtf8;
- }
- /*
- ** Convert UTF-8 to multibyte character string. Space to hold the
- ** returned string is obtained from malloc().
- */
- static char* utf8ToMbcs(const char *zFilename){
- char *zFilenameMbcs;
- WCHAR* zTmpWide;
- zTmpWide = utf8ToUnicode(zFilename);
- if( zTmpWide==0 ){
- return 0;
- }
- zFilenameMbcs = unicodeToMbcs(zTmpWide);
- free(zTmpWide);
- return zFilenameMbcs;
- }
- std::string MbcsToUtf8( const char* pszMbcs )
- {
- std::string str;
- WCHAR *pwchar=0;
- CHAR *pchar=0;
- int len=0;
- int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
- len=MultiByteToWideChar(codepage, 0, pszMbcs, -1, NULL,0);
- pwchar=new WCHAR[len];
- if(pwchar!=0)
- {
- len = MultiByteToWideChar(codepage, 0, pszMbcs, -1, pwchar, len);
- if( len!=0 )
- {
- len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, 0, 0, 0, 0);
- pchar=new CHAR[len];
- if(pchar!=0)
- {
- len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, pchar, len,0, 0);
- if(len!=0)
- {
- str = pchar;
- }
- delete pchar;
- }
- delete pwchar;
- }
- }
- return str;
- }
要测试这些接口,为此我写了一个测试工程,是读取一个xml文件把里面的字符进行转换的,测试工程的代码下载地址如下:
unicode字符和多字节字符的相互转换接口的更多相关文章
- 通过编写串口助手工具学习MFC过程——(三)Unicode字符集的宽字符和多字节字符转换
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- 关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与多字节字符的转换)以及字符串的转换代码测试
#pragma once #include <stdio.h> //getchar() #include <tchar.h> #include <stdlib.h> ...
- 转:Unicode字符集和多字节字符集关系
原文地址: http://my.oschina.net/alphajay/blog/5691 unicode.ucs-2.ucs-4.utf-16.utf-32.utf-8 http://stallm ...
- Unicode字符集和多字节字符集关系
在计算机中字符通常并不是保存为图像,每个字符都是使用一个编码来表示的,而每个字符究竟使用哪个编码代表,要取决于使用哪个字符集(charset). 在最初的时候,Internet上只有一种字符集—— ...
- 使用Unicode(宽字节字符集);多字节字符集中定义宽字节变量
2012-03-25 14:54 (分类:计算机程序) 2.2 宽字符和C 宽字符不一定是Unicode.Unicode是宽字符集的一种.然而,因为本书的焦点是Windows而不是C执行的理论,所以书 ...
- VC++中多字节字符集和Unicode之间的互换
在Visual C++.NET中,默认的字符集是Unicode,这和Windows默认的字符集是一致的,不过在老的VC6.0等工程中,默认的字符集形式是多字节字符集(MBCS:Multi-Byte C ...
- 字符集研究之多字节字符集和unicode字符集
作者:朱金灿 来源:http://blog.csdn.net/clever101 本文简介计算机中两大字符集:多字节字符集和unicode字符集的出现及关系. 首先我们须要明确的是计算机是怎样找到字符 ...
- 关于Unicode,字符集,字符编码,每个程序员都应该知道的事
关于Unicode,字符集,字符编码,每个程序员都应该知道的事 作者:Jack47 李笑来的文章如何判断一个人是否聪明?中提到: 必要.清晰.且准确的概念,是一切思考的基石.所谓思考,很大程度上,就是 ...
- 关于Unicode,字符集,字符编码
基本概念 字符[character] 字符代表了字母表中的字符,标点符号和其他的一些符号.在计算机中,文本是由字符组成的. 字符集合[character set] 由一套用于特定用途的字符组成,例如支 ...
随机推荐
- 24、嵌合体序列Chimeras
转载:http://www.cnblogs.com/xudongliang/p/6497465.html 嵌合体序列:由来自两条或者多条模板链的序列组成,示意图如下: 在PCR反应中,在延伸阶段,由于 ...
- linux学习第一周小结
这几天学习linux课程,安装环境,遇到不会的查询资料,在这个过程中发现了很多有意思的网页,看到了一些不一样的内容,现在对linux的学习兴趣增强了许多.学习解决问题也是很有意思的事情,解决问题的过程 ...
- Dapper.Common基于Dapper的开源LINQ超轻量扩展
Dapper.Common Dapper.Common是基于Dapper的LINQ实现,支持.net core,遵循Linq语法规则.链式调用.配置简单.上手快,支持Mysql,Sqlserver(目 ...
- Note: PANOPLY: Low-TCB Linux Applications with SGX Enclaves
PANOPLY provides middleware for SGX and Linux operating systems. What PANOPLY provides middleware fo ...
- Note: Migratory Compression: Coarse-grained Data Reordering to Improve Compressibility
思路/方法 设计了Migratory Compression. 调整chunk相对位置,将相似chunk排列在一起,通过压缩来减少data store占用的实际存储空间. https://en.wik ...
- Boost Python学习笔记(一)
开发环境搭建 下载源码 boost_1_66_0.tar.gz 生成编译工具 # tar axf boost_1_66_0.tar.gz # cd boost_1_66_0 # yum install ...
- 51nod1428(优先队列)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 题意:中文题诶- 思路:贪心 问最少要多少教室就是求最多 ...
- ALSA声音编程
1. ALSA设备驱动将ALSA设备描述分为四层,从上到下为: default default:0 plughw:0,0 hw:0,0 不同的层次,对设备的控制权限不同,比如hardware para ...
- Filter&Listener
Filter&Listener 内容待补充... ...
- maven项目打包分析及打包后war包缺少配置文件报错的原因分析,使用progard混淆时配置分析
1.maven打包: 一直以来我都没太注意过在myeclipse下使用run as来clean居然对项目的target目录没有进行操作,要让操作有效,需要进入到maven build...选项下,进行 ...