gbk与utf-8转换

时间:2021-07-23 20:16:41

linux:

 1 #include <iconv.h>
 2 
 3 int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
 4 {
 5         iconv_t cd;
 6         int rc;
 7         char **pin = &inbuf;
 8         char **pout = &outbuf;
 9 
10         cd = iconv_open(to_charset,from_charset);
11         if (cd==0)
12                 return -1;
13         memset(outbuf,0,outlen);
14         if (iconv(cd,pin,&inlen,pout,&outlen) == -1)
15                 return -1;
16         iconv_close(cd);
17         return 0;
18 }
19 
20 int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
21 {
22         return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
23 }
24 
25 int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
26 {
27         return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
28 }

解释:

其中

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

(3) int iconv_close(iconv_t cd);
此函数用于关闭转换句柄,释放资源。

Windows:

 1     #include <iostream>  
 2     #include <string>  
 3     #include <fstream>  
 4     #include <windows.h>   
 5       
 6     using namespace std;  
 7       
 8     string GBKToUTF8(const std::string& strGBK)  
 9     {  
10         string strOutUTF8 = "";  
11         WCHAR * str1;  
12         int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);  
13         str1 = new WCHAR[n];  
14         MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);  
15         n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);  
16         char * str2 = new char[n];  
17         WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);  
18         strOutUTF8 = str2;  
19         delete[]str1;  
20         str1 = NULL;  
21         delete[]str2;  
22         str2 = NULL;  
23         return strOutUTF8;  
24     }  
25       
26     string UTF8ToGBK(const std::string& strUTF8)  
27     {  
28         int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);  
29         unsigned short * wszGBK = new unsigned short[len + 1];  
30         memset(wszGBK, 0, len * 2 + 2);  
31         MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len);  
32       
33         len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);  
34         char *szGBK = new char[len + 1];  
35         memset(szGBK, 0, len + 1);  
36         WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);  
37         //strUTF8 = szGBK;  
38         std::string strTemp(szGBK);  
39         delete[]szGBK;  
40         delete[]wszGBK;  
41         return strTemp;  
42     }  
43       
44     int _tmain(int argc, _TCHAR* argv[])  
45     {  
46         string test("我们中国是个强大的名族,强大的动力来自每个人的支持");  
47         fstream output("test.txt",ios_base::out | ios_base::app);  
48         output << GBKToUTF8(test);  
49         //system("iconv -f GBK -t utf-8");  
50         return 0;  
51     }