分享

c中实现utf8和gbk的互转

 wusiqi111 2017-02-14
C代码  收藏代码
  1. #include <iconv.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <unistd.h>  
  5. #include <fcntl.h>  
  6. #include <string.h>  
  7. #include <sys/stat.h>  
  8.   
  9. int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen,  
  10.         char *outbuf, size_t outlen) {  
  11.     iconv_t cd;  
  12.     char **pin = &inbuf;  
  13.     char **pout = &outbuf;  
  14.   
  15.     cd = iconv_open(to_charset, from_charset);  
  16.     if (cd == 0)  
  17.         return -1;  
  18.     memset(outbuf, 0, outlen);  
  19.     if (iconv(cd, pin, &inlen, pout, &outlen) == -1)  
  20.         return -1;  
  21.     iconv_close(cd);  
  22.     *pout = '\0';  
  23.   
  24.     return 0;  
  25. }  
  26.   
  27. int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {  
  28.     return code_convert("utf-8""gb2312", inbuf, inlen, outbuf, outlen);  
  29. }  
  30.   
  31. int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {  
  32.     return code_convert("gb2312""utf-8", inbuf, inlen, outbuf, outlen);  
  33. }  
  34.   
  35. int main(void) {  
  36.     char *s = "中国";  
  37.     int fd = open("test.txt", O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);  
  38.     char buf[10];  
  39.     u2g(s, strlen(s), buf, sizeof(buf));  
  40.     write(fd, buf, strlen(buf));  
  41.     close(fd);  
  42.   
  43.     fd = open("test.txt2", O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);  
  44.     char buf2[10];  
  45.     g2u(buf, strlen(buf), buf2, sizeof(buf2));  
  46.     write(fd, buf2, strlen(buf2));  
  47.     close(fd);  
  48.     return 1;  
  49. }  

 

上面是使用iconv函数。

 

方式二: 使用如下两个函数

mbstowcs将多字节编码转换为宽字节编码

wcstombs将宽字节编码转换为多字节编码

注意, 需要系统编码的支持, 可以通过locale -a 查看系统支持的。若不支持zh_CN.gbk, 需要安装,例如,在ubuntu上的安装步骤如下:

 


编辑

$sudo vi /var/lib/locales/supported.d/zh-hans
更新成
zh_CN.UTF-8 UTF-8
zh_SG.UTF-8 UTF-8
zh_CN.GBK GBK
zh_CN.GB18030 GB18030

 

 

// 更新
$ sudo locale-gen

// 查看
$ locale -a
C
POSIX
zh_CN.gb18030
zh_CN.gbk
zh_CN.utf8
zh_SG.utf8
 
C代码  收藏代码
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <unistd.h>  
  5. #include <fcntl.h>  
  6. #include <sys/stat.h>  
  7. #include <locale.h>  
  8.   
  9. /** 
  10.  * DESCRIPTION: 实现由utf8编码到gbk编码的转换 
  11.  * 
  12.  * Input: gbkStr,转换后的字符串;  srcStr,待转换的字符串; maxGbkStrlen, gbkStr的最 
  13.  大长度 
  14.  * Output: gbkStr 
  15.  * Returns: -1,fail;>0,success 
  16.  * 
  17.  */  
  18. int utf82gbk(char *gbkStr, const char *srcStr, int maxGbkStrlen) {  
  19.     if (NULL == srcStr) {  
  20.         printf("Bad Parameter\n");  
  21.         return -1;  
  22.     }  
  23.   
  24.     //首先先将utf8编码转换为unicode编码  
  25.     if (NULL == setlocale(LC_ALL, "zh_CN.utf8")) //设置转换为unicode前的码,当前为utf8编码  
  26.             {  
  27.         printf("Bad Parameter\n");  
  28.         return -1;  
  29.     }  
  30.   
  31.     int unicodeLen = mbstowcs(NULL, srcStr, 0); //计算转换后的长度  
  32.     if (unicodeLen <= 0) {  
  33.         printf("Can not Transfer!!!\n");  
  34.         return -1;  
  35.     }  
  36.     wchar_t *unicodeStr = (wchar_t *) calloc(sizeof(wchar_t), unicodeLen + 1);  
  37.     mbstowcs(unicodeStr, srcStr, strlen(srcStr)); //将utf8转换为unicode  
  38.   
  39.     //将unicode编码转换为gbk编码  
  40.     if (NULL == setlocale(LC_ALL, "zh_CN.gbk")) //设置unicode转换后的码,当前为gbk  
  41.             {  
  42.         printf("Bad Parameter\n");  
  43.         return -1;  
  44.     }  
  45.     int gbkLen = wcstombs(NULL, unicodeStr, 0); //计算转换后的长度  
  46.     if (gbkLen <= 0) {  
  47.         printf("Can not Transfer!!!\n");  
  48.         return -1;  
  49.     } else if (gbkLen >= maxGbkStrlen) //判断空间是否足够  
  50.             {  
  51.         printf("Dst Str memory not enough\n");  
  52.         return -1;  
  53.     }  
  54.     wcstombs(gbkStr, unicodeStr, gbkLen);  
  55.     gbkStr[gbkLen] = 0; //添加结束符  
  56.     free(unicodeStr);  
  57.     return gbkLen;  
  58. }  
  59.   
  60. int main(void) {  
  61.     char *s = "中国";  
  62.     int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);  
  63.     char buf[10];  
  64.     utf82gbk(buf, s, sizeof(buf));  
  65.     write(fd, buf, strlen(buf));  
  66.     close(fd);  
  67.   
  68.     return 1;  
  69. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多