[C/C++]_[utf8和unicode的相互转换]
2013-10-05 16:28
393 查看
场景:
1.有些库需要接收utf8字符串(也比如数据库表数据),而路径处理则需要unicode字符串,所有在windows下互相转换是必须的。
2.之前自己实现了utf8转unicode,但是并不完美,因为超过4字节的utf8字符串或big endian的字节序支持不好,所以在windows最好的方式应该就是使用系统接口了.
1.有些库需要接收utf8字符串(也比如数据库表数据),而路径处理则需要unicode字符串,所有在windows下互相转换是必须的。
2.之前自己实现了utf8转unicode,但是并不完美,因为超过4字节的utf8字符串或big endian的字节序支持不好,所以在windows最好的方式应该就是使用系统接口了.
#include "windows.h" char* QXUtf82Unicode(const char* utf, size_t *unicode_number) { if(!utf || !strlen(utf)) { *unicode_number = 0; return NULL; } int dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,utf,-1,NULL,0); size_t num = dwUnicodeLen*sizeof(wchar_t); wchar_t *pwText = (wchar_t*)malloc(num); memset(pwText,0,num); MultiByteToWideChar(CP_UTF8,0,utf,-1,pwText,dwUnicodeLen); *unicode_number = dwUnicodeLen - 1; return (char*)pwText; } char* QXUnicode2Utf8(const char* unicode) { int len; len = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, NULL, 0, NULL, NULL); char *szUtf8 = (char*)malloc(len + 1); memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL,NULL); return szUtf8; }
相关文章推荐
- UNICODE 与 UTF8 的相互转换 (c++)版
- [C/C++]_[utf8和unicode的相互转换]
- 【miscellaneous】【C/C++语言】UTF8与GBK字符编码之间的相互转换
- Vc下unicode和UTF8相互转换
- C++中ASCII、unicode与Utf8之间的相互转化
- C++中Ansi、Unicode、UTF8字符串之间的转换和写入文本文件[zz]
- GB2312、UNICODE和UTF8的相互转换
- Java实现的utf8,gbk,unicode编码相互转换的代码
- c++ unicode模式下CString 到string的相互转换
- C/C++中ASCII与Unicode字符串相互转换
- UTF8, ASCII, UNICODE相互转换
- [C/C++]_[utf8字符串转换为unicode字符串]
- C++中ASCII、unicode与Utf8之间的相互转化
- [C/C++]_[utf8字符串转换为unicode字符串]
- C/C++ 各种字符编码字符串的转换 可用于XML文件的字符转化读写 (收集 GBK - UTF8 - Unicode - ANSI )
- C++中Unicode (Big-Endian)和Unicode的相互转换
- MFC 多字节和UNICODE的相互转换(解决中文utf8-ASCNII乱码问题)
- Java实现的utf8,gbk,unicode编码相互转换的代码
- 【Python】utf8,unicode,ascii编码的相互转换
- unicode utf-8 gb2312编码 使用C++相互转换的代码