Linux utf8与gb2312转换函数

Linux下GB2312与UTF8相互转换直接有接口调用的,调用的接口iconv_open

  1. #include <iconv.h>   
  2. iconv_t iconv_open (const char* tocode, const char* fromcode);  

下面看下测试程序:

  1. #include <iostream>     
  2. #include <string>     
  3. #include <errno.h>     
  4. #include <iconv.h>   
  5.   
  6. //iInLen的长度不包括\0,应该用strlen。返回值是处理后的sOut长度     
  7. static int Utf8ToGb2312(char *sOut, int iMaxOutLen, const char *sIn, int iInLen)     
  8. {     
  9.     char *pIn = (char *)sIn;     
  10.     char *pOut = sOut;     
  11.     size_t ret;     
  12.     size_t iLeftLen=iMaxOutLen;     
  13.     iconv_t cd;     
  14.   
  15.     cd = iconv_open("gb2312""utf-8");     
  16.     if (cd == (iconv_t) - 1)     
  17.     {     
  18.         return -1;     
  19.     }     
  20.     size_t iSrcLen=iInLen;     
  21.     ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen);     
  22.     if (ret == (size_t) - 1)     
  23.     {     
  24.         iconv_close(cd);     
  25.         return -1;     
  26.     }     
  27.   
  28.     iconv_close(cd);     
  29.   
  30.     return (iMaxOutLen - iLeftLen);     
  31. }    
  32.   
  33. //iInLen的长度不包括\0,应该用strlen。返回值是处理后的sOut长度     
  34. static int Gb2312ToUtf8(char *sOut, int iMaxOutLen, const char *sIn, int iInLen)     
  35. {     
  36.     char *pIn = (char *)sIn;     
  37.     char *pOut = sOut;     
  38.     size_t ret;     
  39.     size_t iLeftLen=iMaxOutLen;     
  40.     iconv_t cd;     
  41.   
  42.     cd = iconv_open("utf-8""gb2312");     
  43.     if (cd == (iconv_t) - 1)     
  44.     {     
  45.         return -1;     
  46.     }     
  47.     size_t iSrcLen=iInLen;     
  48.     ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen);     
  49.     if (ret == (size_t) - 1)     
  50.     {     
  51.         iconv_close(cd);     
  52.         return -1;     
  53.     }     
  54.   
  55.     iconv_close(cd);     
  56.   
  57.     return (iMaxOutLen - iLeftLen);     
  58. }    
  59.   
  60. int main(void)   
  61. {   
  62.     char* pszOri = "中文字符测试";     
  63.     printf("strlen:%d\n", strlen(pszOri));   
  64.      
  65.     char pszDst[50] = {0};     
  66.     int iLen = Gb2312ToUtf8(pszDst, 50, pszOri, strlen(pszOri)); // Gb2312ToUtf8     
  67.          
  68.     printf("iLen:%d,pszDst=%s\n", iLen, pszDst);   
  69.        
  70.     printf("------------------!\n");   
  71.          
  72.     char pszGbDst[50] = {0};       
  73.     int iNewLen = Utf8ToGb2312(pszGbDst, 50, pszDst, iLen); // Utf8ToGb2312      
  74.     printf("iNewLen:%d,pszGbDst=%s\n", iNewLen, pszGbDst);   
  75.           
  76.     return 0;     
  77. }  

输出结果: 

  1. strlen:12    
  2. 18,涓枃瀛楃娴嬭瘯    
  3. -----------    
  4. 12,中文字符测试    

可以看出,UTF8格式下,一个中文字符占三个字节;而GB2312下占两个字节。

注意:在开发板上转换时记得要带上iconv库.....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值