关于c++中utf8和gbk编码方式的转换
来源:互联网 发布:女朋友水多 体验 知乎 编辑:程序博客网 时间:2024/04/29 21:15
GBK转utf8如下:
string GBKToUTF8(const std::string& strGBK) { string strOutUTF8 = ""; WCHAR * str1; int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0); str1 = new WCHAR[n]; MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n); n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL); char * str2 = new char[n]; WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); strOutUTF8 = str2; delete[]str1; str1 = NULL; delete[]str2; str2 = NULL; return strOutUTF8; }
utf8转GBK如下:
string UTF8ToGBK(const std::string& strUTF8) { int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); unsigned short * wszGBK = new unsigned short[len + 1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK = new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL); //strUTF8 = szGBK; std::string strTemp(szGBK); delete[]szGBK; delete[]wszGBK; return strTemp; }
C++头文件包含文件:
#include <iostream> #include <string> #include <fstream> #include <windows.h>
0 0
- 关于c++中utf8和gbk编码方式的转换
- JS中utf8和GBK的字符编码转换
- UTF8和GBK编码转换
- UTF8 和GBK编码转换
- Qt 中GBK和UTF8的转换
- GBK和UTF8编码之间互相转换的两个函数
- GBK UTF8 编码转换
- UTF8和gbk编码转换(一)
- UTF8和gbk编码转换(二)
- 关于Python2中utf8,gbk,ascii的编码问题
- utf8与gbk字符编码的转换
- GBK和UTF8之间的转换 C语言
- linux c utf8和GBK 的转换函数
- GBK和UTF8之间的转换 C语言
- GBK和UTF8之间的转换 C语言
- PHP中GBK和UTF8编码处理
- PHP中GBK和UTF8编码处理
- PHP中GBK和UTF8编码处理
- 如何返回最底层的Controller
- 数据库的 脏读 不可重复读、幻读
- tomcat结合nginx使用小结 《转载》
- Class not registered(Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)
- 安卓与ios项目
- 关于c++中utf8和gbk编码方式的转换
- Java笔记 ----类和成员、接口的修饰符
- java文件压缩打包操作
- 基于UDP协议的Socket通信
- MyEclipse连接SqlServer 2008的Database Driver配置
- Scala学习整理[第三章 入门Ⅱ]<Programming In Scala>
- 7、Power Query-合并查询
- Cannot run program "svn" (in directory "D:\demo\Test"): CreateProcess error=2, 系统找不到指定的文件。
- SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)