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><?xml version="1.0" encoding="GBK"?><xmlmsg><errmsg></errmsg><vio><rec><jkbj_zw>未缴款</jkbj_zw><jkbj>0</jkbj><jdslb_zw>简易决定书</jdslb_zw><jdslb>1</jdslb><jdsbh>450200170121232</jdsbh><dsr>匡青林</dsr><hpzl>02</hpzl><hpzl_zw>小型汽车</hpzl_zw><hphm>桂BQ9926</hphm><jdcsyr>柳州市驰誉汽车运输有限公司</jdcsyr><jszh>43042619730820901X</jszh><wfsj>2011-01-05 08:27:52</wfsj><wfdz>柳邕路0千米0米</wfdz><clsj>2011-01-05</clsj><cljg>45020400</cljg><cljgmc>柳州市公安局交通警察支队柳南大队</cljgmc><fkje>100</fkje><znj>0</znj></rec></vio></xmlmsg></ns:return></ns:getvioinfoResponse></soapenv:Body></soapenv:Envelope>
0
谁比较熟悉中文编码转换的,这段代码转换中文失败,(自己也不清楚到底是从什么码转到什么码),反正是要显示中文,谁知道该用哪个函数或者该转成哪个编码的?找了不少方法都成功不了~至于(<) 的问题可以不管,这个是对应符号'<' ,这个已经解决,只是这里没有体现。
补充说明一下,上面能看到中文是浏览器转换的,其实是看不到中文的哦。
[ 本帖最后由 khaz 于 2011-11-11 22:08 编辑 ]