gbk与utf-8转换

时间:2023-03-09 08:32:33
gbk与utf-8转换

linux:

 #include <iconv.h>

 int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf; cd = iconv_open(to_charset,from_charset);
if (cd==)
return -;
memset(outbuf,,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen) == -)
return -;
iconv_close(cd);
return ;
} int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
} int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}

解释:

其中

iconv函数族的头文件是iconv.h,使用前需包含之。
#include <iconv.h>
iconv函数族有三个函数,原型如下:
() iconv_t iconv_open(const char *tocode, const char *fromcode);
此函数说明将要进行哪两种编码的转换,tocode是目标编码,fromcode是原编码,该函数返回一个转换句柄,供以下两个函数使用。
() size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);
此函数从inbuf中读取字符,转换后输出到outbuf中,inbytesleft用以记录还未转换的字符数,outbytesleft用以记录输出缓冲的剩余空间。 () int iconv_close(iconv_t cd);
此函数用于关闭转换句柄,释放资源。

Windows:

     #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, , strGBK.c_str(), -, NULL, );
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, , strGBK.c_str(), -, str1, n);
n = WideCharToMultiByte(CP_UTF8, , str1, -, NULL, , NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, , str1, -, 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, , strUTF8.c_str(), -, NULL, );
unsigned short * wszGBK = new unsigned short[len + ];
memset(wszGBK, , len * + );
MultiByteToWideChar(CP_UTF8, , (LPCTSTR)strUTF8.c_str(), -, wszGBK, len); len = WideCharToMultiByte(CP_ACP, , wszGBK, -, NULL, , NULL, NULL);
char *szGBK = new char[len + ];
memset(szGBK, , len + );
WideCharToMultiByte(CP_ACP,, wszGBK, -, 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 ;
}