linux libIconv库实现GBK、Unicode、UTF-8三种编码互转

libIconv库实现GBK、Unicode、UTF-8三种编码互转比window api更为简单,而且libIconv库跨平台。

IibIconv库在windows下的编译参照windows下使用VS编译libIconv库

http://www.gnu.org/software/libiconv/可以查看支持转换的编码类型

下面是几个关键库函数的注释:

(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);
//此函数用于关闭转换句柄,释放资源。

扫描二维码关注公众号,回复: 1707625 查看本文章
  1. #include <iostream>  
  2. #include <string>  
  3. #include <iconv.h>  
  4. using namespace std;  
  5.   
  6. #pragma comment(lib,”libIconv.lib”)  
  7.   
  8. //编码转换,source_charset是源编码,to_charset是目标编码  
  9. std::string code_convert(char source_charset, char *to_charset, const std::string& sourceStr) //sourceStr是源编码字符串  
  10. {  
  11.     iconv_t cd = iconv_open(to_charset, source_charset);//获取转换句柄,void类型  
  12.     if (cd == 0)  
  13.         return “”;  
  14.   
  15.     size_t inlen = sourceStr.size();  
  16.     size_t outlen = 255;  
  17.     char inbuf = (char)sourceStr.c_str();  
  18.     char outbuf[255];//这里实在不知道需要多少个字节,这是个问题  
  19.     //char outbuf = new char[outlen]; 另外outbuf不能在堆上分配内存,否则转换失败,猜测跟iconv函数有关  
  20.     memset(outbuf, 0, outlen);  
  21.   
  22.     char *poutbuf = outbuf; //多加这个转换是为了避免iconv这个函数出现char()[255]类型的实参与char**类型的形参不兼容  
  23.     if (iconv(cd, &inbuf, &inlen, &poutbuf,&outlen) == -1)  
  24.         return “”;  
  25.   
  26.     std::string strTemp(outbuf);//此时的strTemp为转换编码之后的字符串  
  27.     iconv_close(cd);  
  28.     return strTemp;  
  29. }  
  30.   
  31. //gbk转UTF-8    
  32. std::string GbkToUtf8(const std::string& strGbk)// 传入的strGbk是GBK编码   
  33. {  
  34.     return code_convert(“gb2312”“utf-8”,strGbk);  
  35. }  
  36.   
  37. //UTF-8转gbk  
  38. std::string Utf8ToGbk(const std::string& strUtf8)  
  39. {  
  40.     return code_convert(“utf-8”“gb2312”, strUtf8);  
  41. }  
  42.   
  43. //gbk转unicode,”UCS-2LE”代表unicode小端模式  
  44. std::string GbkToUnicode(const std::string& strGbk)// 传入的strGbk是GBK编码   
  45. {  
  46.     return code_convert(“gb2312”“UCS-2LE”,strGbk);  
  47. }  
  48.   
  49. //unicode转gbk  
  50. std::string UnicodeToGbk(const std::string& strGbk)// 传入的strGbk是GBK编码   
  51. {  
  52.     return code_convert(“UCS-2LE”“gb2312”,strGbk);  
  53. }  
  54.   
  55. int main()   
  56. {  
  57.    //1、ANSI/GBK编码    
  58.     string strGbk = ”我”;    
  59.     int num = strGbk.size();//获取两个字符数,也是我字所占的字节数    
  60.     
  61.     unsigned char p = (unsigned char)strGbk.c_str();        
  62.     for (int i = 0; i < num; i++)        
  63.     {        
  64.         printf(”%0x”p);        
  65.         p++;        
  66.     }  //输出ced2 所以我的GBK编码是0xced2    
  67.     printf(”\n”);       
  68.     
  69.     char gbk[] = {0xce, 0xd2, 0x00}; //加上0x00字符串结束符,不会输出乱码    
  70.     cout<<gbk<<endl;//输出汉字我    
  71.     
  72.     
  73.     //2、unicode编码    
  74.     string strUnicode = GbkToUnicode(”我”);//转成unicode编码    
  75.     num = strUnicode.size();  
  76.     p = (unsigned char)strUnicode.c_str();        
  77.     for (int i = 0; i < num; i++)        
  78.     {        
  79.         printf(”%0x”p);        
  80.         p++;        
  81.     }  //输出1162 因为默认是小端模式,所以我的unicode编码是0x6211    
  82.     printf(”\n”);       
  83.     
  84.     char unicode[]= {0x11,0x62, 0x00}; //加上0x00字符串结束符,不会输出乱码    
  85.     cout<<UnicodeToGbk(unicode)<<endl;//需要先将unicode字符串转成gbk之后才能用cout输出    
  86.     
  87.     
  88.     //3、UTF-8编码    
  89.     string strUtf8  = GbkToUtf8(”我”);//转成utf8编码    
  90.     num = strUtf8.size();//num=3    
  91.     p = (unsigned char)strUtf8.c_str();        
  92.     for (int i = 0; i < num; i++)        
  93.     {        
  94.         printf(”%0x”, *p);        
  95.         p++;        
  96.     }  //输出e68891    
  97.     printf(”\n”);       
  98.     
  99.     char utf8[] = {0xe6, 0x88, 0x91,0x00}; //加上0x00字符串结束符,不会输出乱码    
  100.     cout<<Utf8ToGbk(utf8)<<endl;//需要先将utf8字符串转成gbk之后才能用cout输出    
  101.   
  102.     return 0;  
  103. }  

猜你喜欢

转载自blog.csdn.net/wushuangge/article/details/78909138