linux 字符串编码转换

 

 1 #include <iconv.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 
 6 //*****************************************************************
 7 #define buf_length 128
 8 # define UTF_8 "utf-8"
 9 #define GB2312  "gb2312"
10 //*****************************************************************
11 int code_convert_process(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen){
12     iconv_t cd;
13     int rc;
14     char **pin = &inbuf;
15     char **pout = &outbuf;
16     cd = iconv_open(to_charset,from_charset);
17     if (cd==0) return -1;
18     memset(outbuf,0,outlen);
19     if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
20     iconv_close(cd);
21     return 0;
22 }
23 
24 #define UTF8_to_GB3212(in_buf,in_len,out_buf,out_len)   code_convert_process(UTF_8,GB2312,in_buf,in_len,out_buf,out_len)
25 #define GB3212_to_UTF8(in_buf,in_len,out_buf,out_len)   code_convert_process(GB2312,UTF_8,in_buf,in_len,out_buf,out_len)
26 //*****************************************************************
27 int main(void){
28     char in_buf[buf_length] = "jikexianfeng@极客先锋";
29     char out_buf[buf_length];
30     UTF8_to_GB3212(in_buf,strlen(in_buf),out_buf,buf_length);
31     //GB3212_to_UTF8(in_buf,strlen(in_buf),out_buf,buf_length);
32 
33   return 0;
34 }

 

posted @ 2020-06-26 11:09  极客先锋  阅读(583)  评论(0编辑  收藏  举报