方法有3种:
1.前面有一篇关于linux下wchar_t的,可以参考一下,这个是最笨的办法,在实际用处中少用。
2.前面的一篇关于libiconv的,利用libiconv进行编码转换在linux下士常见的,
cd = iconv_open(to_charset,from_charset);
iconv(cd,pin,&inlen,pout,&outlen)
iconv_close(cd);
这三个函数可以的,写了一个测试程序
#include
#include
#include
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1)
return -1;
iconv_close(cd);
return 0;
}
int u2g(char *inbuf,int inlen,char *outbuf,int outlen) //UNICODE码转为GB2312码
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen) //GB2312码转为UNICODE码
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}
int main(void)
{
unsigned char in_utf8[] = {0x20,0xe8,0xbf,0x99,0xe6,0x98,0xaf,0xe4,0xb8,0x80,0xe4,0xb8,0xaa,0x69,0x63,0x6f,0x6e,0x76,0xe7,0x9a,0x84,0xe6,0xb5,0x8b,0xe8,0xaf,0x95,0xe4,0xbe,0x8b,0xe7,0xa8,0x8b,0xef,0xbc,0x9a,0xa,0xe2,0x80,0x9c,0xe7,0xba,0xb5,0xe6,0xb5,0xb7,0xe6,0x9d,0xaf,0xe2,0x80,0x9d,0xe4,0xb8,0x9c,0xe5,0x8d,0x97,0xe5,0xa4,0xa7,0xe5,0xad,0xa6,0xe7,0xac,0xac,0xe4,0xb8,0x89,0xe5,0xb1,0x8a,0xe5,0xb5,0x8c,0xe5,0x85,0xa5,0xe5,0xbc,0x8f,0xe7,0xb3,0xbb,0xe7,0xbb,0x9f,0xe8,0xae,0xbe,0xe8,0xae,0xa1,0xe9,0x82,0x80,0xe8,0xaf,0xb7,0xe8,0xb5,0x9b}; //UTF-8编码
char out[100];
int rc;
rc = u2g(in_utf8,strlen(in_utf8),out,100);
printf("%s\n",out);
}
在PC linux上可以,由于libiconv库没有移植好,页不知道哦是不是没有移植好,就是编译的时候说未定义libiconv。。。。但现在还没解决问题所在。
虽然在libc.so.6利民包含了iconv等函数,但是调用返回的结果并不是理想的结果。
3.利用mbsrtowcs一次性编码转换,前提是你的系统要支持中文,程序中要加入:setlocale(LC_ALL, "zh_CN.gb18030"); 在PC linux下可以,板子上gb18030安装上了,但是就是mbsrtowcs返回-1,就是说系统不支持改编码字符,应该是环境没搭建好。
哎。。搞了中文环境搭建和中文转unicode搞了3天,没啥结果,气死了,我的牙龈又肿了了,心情很是不爽,昨晚2点多才睡着。苦命的程序员屌丝。。不过昨晚还LG了一把,。。。爽了一把。