小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c中實(shí)現(xiàn)utf8和gbk的互轉(zhuǎn)

 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函數(shù)。

 

方式二: 使用如下兩個函數(shù)

mbstowcs將多字節(jié)編碼轉(zhuǎn)換為寬字節(jié)編碼

wcstombs將寬字節(jié)編碼轉(zhuǎn)換為多字節(jié)編碼

注意, 需要系統(tǒng)編碼的支持, 可以通過locale -a 查看系統(tǒng)支持的。若不支持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: 實(shí)現(xiàn)由utf8編碼到gbk編碼的轉(zhuǎn)換 
  11.  * 
  12.  * Input: gbkStr,轉(zhuǎn)換后的字符串;  srcStr,待轉(zhuǎn)換的字符串; 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編碼轉(zhuǎn)換為unicode編碼  
  25.     if (NULL == setlocale(LC_ALL, "zh_CN.utf8")) //設(shè)置轉(zhuǎn)換為unicode前的碼,當(dāng)前為utf8編碼  
  26.             {  
  27.         printf("Bad Parameter\n");  
  28.         return -1;  
  29.     }  
  30.   
  31.     int unicodeLen = mbstowcs(NULL, srcStr, 0); //計算轉(zhuǎn)換后的長度  
  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轉(zhuǎn)換為unicode  
  38.   
  39.     //將unicode編碼轉(zhuǎn)換為gbk編碼  
  40.     if (NULL == setlocale(LC_ALL, "zh_CN.gbk")) //設(shè)置unicode轉(zhuǎn)換后的碼,當(dāng)前為gbk  
  41.             {  
  42.         printf("Bad Parameter\n");  
  43.         return -1;  
  44.     }  
  45.     int gbkLen = wcstombs(NULL, unicodeStr, 0); //計算轉(zhuǎn)換后的長度  
  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; //添加結(jié)束符  
  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. }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多