GBK与UTF-8 之间的相互转换
来源:互联网 发布:苹果5s蜂窝数据打不开 编辑:程序博客网 时间:2024/05/22 05:28
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[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;
}
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);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
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;
}
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);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}
转自:http://www.cppblog.com/zgysx/articles/13085.html
阅读全文
0 0
- GBK与UTF-8 之间的相互转换
- GBK与UTF-8之间的转换
- GBK与UTF-8之间的转换
- UTF-8与GBK之间的转换
- utf-8和gbk之间相互转换
- GBK和UTF-8的区别与相互转换
- utf-8与unicode之间的相互转换
- utf-8与gbk相互转换c++实现
- UTF-8与GBK之间的区别
- vc中gbk到utf-8的相互转换
- 简要介绍一下UTF-8编码和GBK编码------并利用linux中的iconv命令来实现UTF-8和GBK之间的相互转换
- utf-8与gbk的转换
- UTF8与GBK字符编码之间的相互转换
- UTF8与GBK字符编码之间的相互转换
- UTF8与GBK字符编码之间的相互转换
- 【编码与乱码】(05)---GBK与UTF-8之间的转换
- 编码与乱码(05)-GBK与UTF-8之间的转换
- 【Java基础专题】编码与乱码(05)---GBK与UTF-8之间的转换
- 23. Merge k Sorted Lists
- Android线程间通信Handler机制(Android开发艺术探索学习笔记)
- iOS 横竖屏切换
- 阿里巴巴Java开发手册- 编程规约-命名规约
- 线程池动态并安全地中断所有线程不需要使用shutdownnow()同时可以继续添加任务
- GBK与UTF-8 之间的相互转换
- Dubbo——各协议暴露和引用服务的逻辑
- Android 仿火萤视频桌面 神奇的LiveWallPaper
- 浅析Android情景模式流程
- HDU 5894 hannnnah_j’s Biological Test(组合数学 逆元)
- RecyclerView用法总结
- 凸包面积(0249)
- Educational Codeforces Round 20 F. Coprime Subsequences(莫比乌斯反演)
- Android WebView与JS页面相互调用(二)