GBK与UTF-8 之间的相互转换
2017-05-15 16:26
253 查看
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);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}
转自:http://www.cppblog.com/zgysx/articles/13085.html
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);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}
转自:http://www.cppblog.com/zgysx/articles/13085.html
相关文章推荐
- dedecms网站GBK与UTF-8相互之间怎么转换
- 简要介绍一下UTF-8编码和GBK编码------并利用linux中的iconv命令来实现UTF-8和GBK之间的相互转换
- utf-8和gbk之间相互转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- 【转】Java编码与乱码---GBK与UTF-8之间的转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- 编码与乱码(05)-GBK与UTF-8之间的转换
- Java实现的UTF-8,GBK,Unicode编码相互转换的代码
- utf-8与unicode之间的相互转换
- 第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- 利用 libiconv 实现汉字编码 utf-8 格式 和 gbk格式的相互转换
- Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- UTF-8,UNICODE,Gb2312他们3个之间的相互转换.
- 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- iOS普通字符串和UTF-8之间的相互转换
- C#_汉字与GBK,Unicode,UTF-8编码之间的转换(by daode1212)
- GB2312、UTF_8、UNICODE之间相互转换
- GBK与UTF-8之间的转换