字符集GBK到UTF8的转换

时间:2022-10-16 14:07:52
http://bbs.csdn.net/topics/390196457
#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;


string GBKToUTF8(const 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;
}

string strXml = "<?xml version=\"1.0\">\n\
<Action>\n\
\t<Variable>这是一个测试</Variable>\n\
\t<Query>\n\
\t\t<Address>阿防盗斯</Address>\n\
\t</Query>\n\
</Action>\n";

int main()
{
	cout << strXml << endl;
	cout << "\n---------------------------------\n" << endl;
	cout << GBKToUTF8(strXml) << endl;
	return 0;
}

http://bbs.csdn.net/topics/390196457

iconv是跨平台的,windows,linux...都可以用


#include <iconv.h>
#pragma comment(lib,"iconv.lib")
 
int code_convert(char *from_charset,char *to_charset,const char *inbuf, size_t inlen,char *outbuf, size_t outlen)
{
         iconv_t cd;
         const char **pin = &inbuf;
         char **pout = &outbuf;
 
         cd = iconv_open(to_charset,from_charset);
         if (cd==0) return -1;
         memset(outbuf,0,outlen);
         if (iconv(cd, pin, &inlen,pout, &outlen)==-1) return -1;
         iconv_close(cd);
         return 0;
}
 
/* UTF-8 to GBK  */
int u2g(const char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
         return code_convert("UTF-8","GBK",inbuf,inlen,outbuf,outlen);
}
 
/* GBK to UTF-8 */
int g2u(const char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
         return code_convert("GBK", "UTF-8", inbuf, inlen, outbuf, outlen);
}


http://blog.csdn.net/p569354158/article/details/6567175

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h> 

using namespace std;

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;
}

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;
}

int _tmain(int argc, _TCHAR* argv[])
{
	string test("我们中国是个强大的名族,强大的动力来自每个人的支持");
	fstream output("test.txt",ios_base::out | ios_base::app);
	output << GBKToUTF8(test);
	//system("iconv -f GBK -t utf-8");
	return 0;
}