Linux字符串转码utf8,Linux C/C++ 字符集转换,UTF-8,GB2312

  • Post author:
  • Post category:linux


Linux C 字符集转换,UTF-8,GB2312

最近帮朋友写个系统接口的小东东,2个系统字符集不同,一个采用UTF-8,一个采用GB2312,不得已需要转换字符集。转换函数记录如下:

#include

#include

#include

#include

#define OUTLEN 255

main()

{

char *in_utf8 = “utf8字符串”;

char *in_gb2312 = “\xbe\xb2\xcc\xac\xc4\xa3\xca\xbd”;

char out[OUTLEN];

int rec ;

//unicode码转为gb2312码

rec = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);

printf(“unicode–>gb2312 out=%s\n”,out);

//gb2312码转为unicode码

rec = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);

printf(“gb2312–>unicode out=%s \n”,out);

}

//代码转换:从一种编码转为另一种编码

int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)

{

iconv_t cd;

int rc;