| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1979 人关注过本帖
标题:linux下中文编码转换问题
只看楼主 加入收藏
khaz
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:130
专家分:188
注 册:2011-4-21
结帖率:77.78%
收藏
已结贴  问题点数:50 回复次数:2 
linux下中文编码转换问题
程序代码:
#include<stdio.h>
#include "/usr/include/iconv.h"



int main()
{
    char src[4096]={0};
    char dest[4096]={0};
    char peer1_0[] = {
0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x3a,
0x20, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2d,
0x43, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x2f, 0x31,
0x2e, 0x31, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74,
0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x78,
0x6d, 0x6c, 0x3b, 0x63, 0x68, 0x61, 0x72, 0x73,
0x65, 0x74, 0x3d, 0x47, 0x42, 0x4b, 0x0d, 0x0a,
0x54, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x65, 0x72,
0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e,
0x67, 0x3a, 0x20, 0x63, 0x68, 0x75, 0x6e, 0x6b,
0x65, 0x64, 0x0d, 0x0a, 0x44, 0x61, 0x74, 0x65,
0x3a, 0x20, 0x46, 0x72, 0x69, 0x2c, 0x20, 0x31,
0x31, 0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30,
0x31, 0x31, 0x20, 0x31, 0x31, 0x3a, 0x35, 0x31,
0x3a, 0x33, 0x37, 0x20, 0x47, 0x4d, 0x54, 0x0d,
0x0a, 0x0d, 0x0a,
0x34, 0x64, 0x32, 0x0d, 0x0a ,
0x3c, 0x3f, 0x78, 0x6d, 0x6c, 0x20, 0x76, 0x65,
0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x27, 0x31,
0x2e, 0x30, 0x27, 0x20, 0x65, 0x6e, 0x63, 0x6f,
0x64, 0x69, 0x6e, 0x67, 0x3d, 0x27, 0x47, 0x42,
0x4b, 0x27, 0x3f, 0x3e, 0x3c, 0x73, 0x6f, 0x61,
0x70, 0x65, 0x6e, 0x76, 0x3a, 0x45, 0x6e, 0x76,
0x65, 0x6c, 0x6f, 0x70, 0x65, 0x20, 0x78, 0x6d,
0x6c, 0x6e, 0x73, 0x3a, 0x73, 0x6f, 0x61, 0x70,
0x65, 0x6e, 0x76, 0x3d, 0x22, 0x68, 0x74, 0x74,
0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x63, 0x68, 0x65,
0x6d, 0x61, 0x73, 0x2e, 0x78, 0x6d, 0x6c, 0x73,
0x6f, 0x61, 0x70, 0x2e, 0x6f, 0x72, 0x67, 0x2f,
0x73, 0x6f, 0x61, 0x70, 0x2f, 0x65, 0x6e, 0x76,
0x65, 0x6c, 0x6f, 0x70, 0x65, 0x2f, 0x22, 0x3e,
0x3c, 0x73, 0x6f, 0x61, 0x70, 0x65, 0x6e, 0x76,
0x3a, 0x42, 0x6f, 0x64, 0x79, 0x3e, 0x3c, 0x6e,
0x73, 0x3a, 0x67, 0x65, 0x74, 0x76, 0x69, 0x6f,
0x69, 0x6e, 0x66, 0x6f, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x20, 0x78, 0x6d, 0x6c,
0x6e, 0x73, 0x3a, 0x6e, 0x73, 0x3d, 0x22, 0x68,
0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x73,
0x22, 0x3e, 0x3c, 0x6e, 0x73, 0x3a, 0x72, 0x65,
0x74, 0x75, 0x72, 0x6e, 0x3e, 0x26, 0x6c, 0x74,
0x3b, 0x3f, 0x78, 0x6d, 0x6c, 0x20, 0x76, 0x65,
0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x22, 0x31,
0x2e, 0x30, 0x22, 0x20, 0x65, 0x6e, 0x63, 0x6f,
0x64, 0x69, 0x6e, 0x67, 0x3d, 0x22, 0x47, 0x42,
0x4b, 0x22, 0x3f, 0x3e, 0x26, 0x6c, 0x74, 0x3b,
0x78, 0x6d, 0x6c, 0x6d, 0x73, 0x67, 0x3e, 0x26,
0x6c, 0x74, 0x3b, 0x65, 0x72, 0x72, 0x6d, 0x73,
0x67, 0x3e, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x65,
0x72, 0x72, 0x6d, 0x73, 0x67, 0x3e, 0x26, 0x6c,
0x74, 0x3b, 0x76, 0x69, 0x6f, 0x3e, 0x26, 0x6c,
0x74, 0x3b, 0x72, 0x65, 0x63, 0x3e, 0x26, 0x6c,
0x74, 0x3b, 0x6a, 0x6b, 0x62, 0x6a, 0x5f, 0x7a,
0x77, 0x3e, 0x26, 0x23, 0x78, 0x36, 0x37, 0x32,
0x61, 0x3b, 0x26, 0x23, 0x78, 0x37, 0x66, 0x33,
0x34, 0x3b, 0x26, 0x23, 0x78, 0x36, 0x62, 0x33,
0x65, 0x3b, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x6a,
0x6b, 0x62, 0x6a, 0x5f, 0x7a, 0x77, 0x3e, 0x26,
0x6c, 0x74, 0x3b, 0x6a, 0x6b, 0x62, 0x6a, 0x3e,
0x30, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x6a, 0x6b,
0x62, 0x6a, 0x3e, 0x26, 0x6c, 0x74, 0x3b, 0x6a,
0x64, 0x73, 0x6c, 0x62, 0x5f, 0x7a, 0x77, 0x3e,
0x26, 0x23, 0x78, 0x37, 0x62, 0x38, 0x30, 0x3b,
0x26, 0x23, 0x78, 0x36, 0x36, 0x31, 0x33, 0x3b,
0x26, 0x23, 0x78, 0x35, 0x31, 0x62, 0x33, 0x3b,
0x26, 0x23, 0x78, 0x35, 0x62, 0x39, 0x61, 0x3b,
0x26, 0x23, 0x78, 0x34, 0x65, 0x36, 0x36, 0x3b,
0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x6a, 0x64, 0x73,
0x6c, 0x62, 0x5f, 0x7a, 0x77, 0x3e, 0x26, 0x6c,
0x74, 0x3b, 0x6a, 0x64, 0x73, 0x6c, 0x62, 0x3e,
0x31, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x6a, 0x64,
0x73, 0x6c, 0x62, 0x3e, 0x26, 0x6c, 0x74, 0x3b,
0x6a, 0x64, 0x73, 0x62, 0x68, 0x3e, 0x34, 0x35,
0x30, 0x32, 0x30, 0x30, 0x31, 0x37, 0x30, 0x31,
0x32, 0x31, 0x32, 0x33, 0x32, 0x26, 0x6c, 0x74,
0x3b, 0x2f, 0x6a, 0x64, 0x73, 0x62, 0x68, 0x3e,
0x26, 0x6c, 0x74, 0x3b, 0x64, 0x73, 0x72, 0x3e,
0x26, 0x23, 0x78, 0x35, 0x33, 0x32, 0x31, 0x3b,
0x26, 0x23, 0x78, 0x39, 0x37, 0x35, 0x32, 0x3b,
0x26, 0x23, 0x78, 0x36, 0x37, 0x39, 0x37, 0x3b,
0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x64, 0x73, 0x72,
0x3e, 0x26, 0x6c, 0x74, 0x3b, 0x68, 0x70, 0x7a,
0x6c, 0x3e, 0x30, 0x32, 0x26, 0x6c, 0x74, 0x3b,
0x2f, 0x68, 0x70, 0x7a, 0x6c, 0x3e, 0x26, 0x6c,
0x74, 0x3b, 0x68, 0x70, 0x7a, 0x6c, 0x5f, 0x7a,
0x77, 0x3e, 0x26, 0x23, 0x78, 0x35, 0x63, 0x30,
0x66, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x37, 0x38,
0x62, 0x3b, 0x26, 0x23, 0x78, 0x36, 0x63, 0x37,
0x64, 0x3b, 0x26, 0x23, 0x78, 0x38, 0x66, 0x36,
0x36, 0x3b, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x68,
0x70, 0x7a, 0x6c, 0x5f, 0x7a, 0x77, 0x3e, 0x26,
0x6c, 0x74, 0x3b, 0x68, 0x70, 0x68, 0x6d, 0x3e,
0x26, 0x23, 0x78, 0x36, 0x38, 0x34, 0x32, 0x3b,
0x42, 0x51, 0x39, 0x39, 0x32, 0x36, 0x26, 0x6c,
0x74, 0x3b, 0x2f, 0x68, 0x70, 0x68, 0x6d, 0x3e,
0x26, 0x6c, 0x74, 0x3b, 0x6a, 0x64, 0x63, 0x73,
0x79, 0x72, 0x3e, 0x26, 0x23, 0x78, 0x36, 0x37,
0x66, 0x33, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x64,
0x64, 0x65, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x65,
0x30, 0x32, 0x3b, 0x26, 0x23, 0x78, 0x39, 0x61,
0x37, 0x30, 0x3b, 0x26, 0x23, 0x78, 0x38, 0x61,
0x38, 0x39, 0x3b, 0x26, 0x23, 0x78, 0x36, 0x63,
0x37, 0x64, 0x3b, 0x26, 0x23, 0x78, 0x38, 0x66,
0x36, 0x36, 0x3b, 0x26, 0x23, 0x78, 0x38, 0x66,
0x64, 0x30, 0x3b, 0x26, 0x23, 0x78, 0x38, 0x66,
0x39, 0x33, 0x3b, 0x26, 0x23, 0x78, 0x36, 0x37,
0x30, 0x39, 0x3b, 0x26, 0x23, 0x78, 0x39, 0x36,
0x35, 0x30, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x31,
0x36, 0x63, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x33,
0x66, 0x38, 0x3b, 0x26, 0x6c, 0x74, 0x3b, 0x2f,
0x6a, 0x64, 0x63, 0x73, 0x79, 0x72, 0x3e, 0x26,
0x6c, 0x74, 0x3b, 0x6a, 0x73, 0x7a, 0x68, 0x3e,
0x34, 0x33, 0x30, 0x34, 0x32, 0x36, 0x31, 0x39,
0x37, 0x33, 0x30, 0x38, 0x32, 0x30, 0x39, 0x30,
0x31, 0x58, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x6a,
0x73, 0x7a, 0x68, 0x3e, 0x26, 0x6c, 0x74, 0x3b,
0x77, 0x66, 0x73, 0x6a, 0x3e, 0x32, 0x30, 0x31,
0x31, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x35, 0x20,
0x30, 0x38, 0x3a, 0x32, 0x37, 0x3a, 0x35, 0x32,
0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x77, 0x66, 0x73,
0x6a, 0x3e, 0x26, 0x6c, 0x74, 0x3b, 0x77, 0x66,
0x64, 0x7a, 0x3e, 0x26, 0x23, 0x78, 0x36, 0x37,
0x66, 0x33, 0x3b, 0x26, 0x23, 0x78, 0x39, 0x30,
0x39, 0x35, 0x3b, 0x26, 0x23, 0x78, 0x38, 0x64,
0x65, 0x66, 0x3b, 0x30, 0x26, 0x23, 0x78, 0x35,
0x33, 0x34, 0x33, 0x3b, 0x26, 0x23, 0x78, 0x37,
0x63, 0x37, 0x33, 0x3b, 0x30, 0x26, 0x23, 0x78,
0x37, 0x63, 0x37, 0x33, 0x3b, 0x26, 0x6c, 0x74,
0x3b, 0x2f, 0x77, 0x66, 0x64, 0x7a, 0x3e, 0x26,
0x6c, 0x74, 0x3b, 0x63, 0x6c, 0x73, 0x6a, 0x3e,
0x32, 0x30, 0x31, 0x31, 0x2d, 0x30, 0x31, 0x2d,
0x30, 0x35, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x63,
0x6c, 0x73, 0x6a, 0x3e, 0x26, 0x6c, 0x74, 0x3b,
0x63, 0x6c, 0x6a, 0x67, 0x3e, 0x34, 0x35, 0x30,
0x32, 0x30, 0x34, 0x30, 0x30, 0x26, 0x6c, 0x74,
0x3b, 0x2f, 0x63, 0x6c, 0x6a, 0x67, 0x3e, 0x26,
0x6c, 0x74, 0x3b, 0x63, 0x6c, 0x6a, 0x67, 0x6d,
0x63, 0x3e, 0x26, 0x23, 0x78, 0x36, 0x37, 0x66,
0x33, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x64, 0x64,
0x65, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x65, 0x30,
0x32, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x31, 0x36,
0x63, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x62, 0x38,
0x39, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x63, 0x34,
0x30, 0x3b, 0x26, 0x23, 0x78, 0x34, 0x65, 0x61,
0x34, 0x3b, 0x26, 0x23, 0x78, 0x39, 0x30, 0x31,
0x61, 0x3b, 0x26, 0x23, 0x78, 0x38, 0x62, 0x36 ,
0x36, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x62, 0x64,
0x66, 0x3b, 0x26, 0x23, 0x78, 0x36, 0x35, 0x32,
0x66, 0x3b, 0x26, 0x23, 0x78, 0x39, 0x36, 0x31,
0x66, 0x3b, 0x26, 0x23, 0x78, 0x36, 0x37, 0x66,
0x33, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x33, 0x35,
0x37, 0x3b, 0x26, 0x23, 0x78, 0x35, 0x39, 0x32,
0x37, 0x3b, 0x26, 0x23, 0x78, 0x39, 0x36, 0x31,
0x66, 0x3b, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x63,
0x6c, 0x6a, 0x67, 0x6d, 0x63, 0x3e, 0x26, 0x6c,
0x74, 0x3b, 0x66, 0x6b, 0x6a, 0x65, 0x3e, 0x31,
0x30, 0x30, 0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x66,
0x6b, 0x6a, 0x65, 0x3e, 0x26, 0x6c, 0x74, 0x3b,
0x7a, 0x6e, 0x6a, 0x3e, 0x30, 0x26, 0x6c, 0x74,
0x3b, 0x2f, 0x7a, 0x6e, 0x6a, 0x3e, 0x26, 0x6c,
0x74, 0x3b, 0x2f, 0x72, 0x65, 0x63, 0x3e, 0x26,
0x6c, 0x74, 0x3b, 0x2f, 0x76, 0x69, 0x6f, 0x3e,
0x26, 0x6c, 0x74, 0x3b, 0x2f, 0x78, 0x6d, 0x6c,
0x6d, 0x73, 0x67, 0x3e, 0x3c, 0x2f, 0x6e, 0x73,
0x3a, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x3e,
0x3c, 0x2f, 0x6e, 0x73, 0x3a, 0x67, 0x65, 0x74,
0x76, 0x69, 0x6f, 0x69, 0x6e, 0x66, 0x6f, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x3e,
0x3c, 0x2f, 0x73, 0x6f, 0x61, 0x70, 0x65, 0x6e,
0x76, 0x3a, 0x42, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
0x2f, 0x73, 0x6f, 0x61, 0x70, 0x65, 0x6e, 0x76,
0x3a, 0x45, 0x6e, 0x76, 0x65, 0x6c, 0x6f, 0x70,
0x65, 0x3e ,
0x0d, 0x0a ,
0x30, 0x0d, 0x0a, 0x0d, 0x0a };
    memcpy(src, peer1_0, sizeof(peer1_0));
    u2g(src, 4096, dest, 4096);
    fputs(dest, stdout);
    return 0;
}

//代码转换:从一种编码转为另一种编码
int code_convert(char *from_charset, char *to_charset, char *inbuf,
         int inlen, char *outbuf, int outlen)
{
   iconv_t cd;
   int rc;
   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;
}

//UNICODE码转为GB2312码
int u2g(char *inbuf, int inlen, char *outbuf, int outlen)
{
   return code_convert("UTF-8", "GB2312", inbuf, inlen, outbuf, outlen);
}

//GB2312码转为UNICODE码
int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
   return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}
运行结果:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=GBK
Transfer-Encoding: chunked
Date: Fri, 11 Nov 2011 11:51:37 GMT

4d2
<?xml version='1.0' encoding='GBK'?><soapenv:Envelope xmlns:soapenv="http://schemas. xmlns:ns="http://ws"><ns:return>&lt;?xml version="1.0" encoding="GBK"?>&lt;xmlmsg>&lt;errmsg>&lt;/errmsg>&lt;vio>&lt;rec>&lt;jkbj_zw>未缴款&lt;/jkbj_zw>&lt;jkbj>0&lt;/jkbj>&lt;jdslb_zw>简易决定书&lt;/jdslb_zw>&lt;jdslb>1&lt;/jdslb>&lt;jdsbh>450200170121232&lt;/jdsbh>&lt;dsr>匡青林&lt;/dsr>&lt;hpzl>02&lt;/hpzl>&lt;hpzl_zw>小型汽车&lt;/hpzl_zw>&lt;hphm>桂BQ9926&lt;/hphm>&lt;jdcsyr>柳州市驰誉汽车运输有限公司&lt;/jdcsyr>&lt;jszh>43042619730820901X&lt;/jszh>&lt;wfsj>2011-01-05 08:27:52&lt;/wfsj>&lt;wfdz>柳邕路0千米0米&lt;/wfdz>&lt;clsj>2011-01-05&lt;/clsj>&lt;cljg>45020400&lt;/cljg>&lt;cljgmc>柳州市公安局交通警察支队柳南大队&lt;/cljgmc>&lt;fkje>100&lt;/fkje>&lt;znj>0&lt;/znj>&lt;/rec>&lt;/vio>&lt;/xmlmsg></ns:return></ns:getvioinfoResponse></soapenv:Body></soapenv:Envelope>
0


谁比较熟悉中文编码转换的,这段代码转换中文失败,(自己也不清楚到底是从什么码转到什么码),反正是要显示中文,谁知道该用哪个函数或者该转成哪个编码的?找了不少方法都成功不了~至于(&lt;) 的问题可以不管,这个是对应符号'<' ,这个已经解决,只是这里没有体现。
补充说明一下,上面能看到中文是浏览器转换的,其实是看不到中文的哦。

[ 本帖最后由 khaz 于 2011-11-11 22:08 编辑 ]
搜索更多相关主题的帖子: linux 中文 
2011-11-11 21:59
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:50 
在浏览器中,编码的方式体现在两个地方:
1、http应答中;
2、在html文件中;
如果两个地方同时存在,以html文件中为基准。
取得文件传输编码后,你对照自己的终端能支持哪种字库,相应转换编码显示即可。
从你提供的例子看,源码中使用的编码是GBK,GBK是一种带繁体的泛汉字字库。如果你需要直接看到,建议你转成gb2312编码,国内编辑器默认的编码。
采用那个函数你可以查查,以前我们是自己写的函数,对照码表做到。

有不清楚之处,欢迎讨论。
2011-11-12 17:01
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:0 
补充,你这段代码,需要先有个html的解析,解析出来的汉字编码才能转码。汉字的编码有特定的格式,你可以查查。
html解析源码,网上可以找到。
在测试过程中,先用特定汉字的特定编码实验,尽量不要一下子扔进去一个连自己都不懂的大数据。
2011-11-12 17:06
快速回复:linux下中文编码转换问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044819 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved