UTF8与GBK间的转换
2015-09-25 10:07
344 查看
1. 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 ; 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 ; WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); strOutUTF8 = str2; delete[]str1; str1 = NULL; delete[]str2; str2 = NULL; return strOutUTF8; }
2. 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); std::string strTemp(szGBK); delete[]szGBK; delete[]wszGBK; return strTemp; }
相关文章推荐
- Linux下XWindow图形界面的基本概念
- UTF-8、Unicode 标准化表单、BOM
- ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)第1/2页
- ASP+FSO生成的网页文件默认编码格式以及转换成UTF-8编码方法
- php中json_encode UTF-8中文乱码的更好解决方法
- 判断window.onload是否多次使用的方法
- Javascript中封装window.open解决不兼容问题
- jquery自定义插件开发之window的实现过程
- php utf-8转unicode的函数第1/2页
- window.returnValue使用方法示例介绍
- utf-8编码引起js输出中文乱码的解决办法
- Shell脚本把文件从GBK转为UTF-8编码
- jq的get传参数在utf-8中乱码问题的解决php版
- php UTF-8、Unicode和BOM问题
- window系统的Rsync同步实战分析第1/2页
- php中文乱码怎么办如何让浏览器自动识别utf-8
- JavaScript中window、doucment、body的解释
- JavaScript的document对象和window对象详解
- 将字符串转换成gb2312或者utf-8编码的参数(js版)
- window.onload和$(function(){})的区别介绍