- public static string GetEncodingString(string srcString)
- {
- Encoding e8859Encode = Encoding.GetEncoding("iso-8859-1");
- Encoding srcEncode = Encoding.GetEncoding("gb2312");
- Encoding dstEncode = Encoding.Unicode;
- byte[] srcBytes = e8859Encode.GetBytes(srcString);//用iso-8859-1去转换源字符串
- byte[] dstBytes = Encoding.Convert(srcEncode, dstEncode, srcBytes);//但是,是从gb2312转到unicode的
- char[] dstChars = new char[dstEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
- dstEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);
- return new string(dstChars);
-
- }
具体原因我推测是因为数据过来是ISO-8859-1,但.Net地层格处理成了Gb2312了,所以需要通过两种编码转换成Unicode才能正常显示,听说在Java中只要new String(s.getBytes("ISO-8859-1"),"gb2312");就OK了。
转换成ISO
- public static string GetISOEncodedString(string srcString)
- {
- if(bDBInfor) {
- Encoding e8859Encode = Encoding.GetEncoding("iso-8859-1");
- Encoding srcEncode = Encoding.Unicode;
- Encoding dstEncode = Encoding.GetEncoding("gb2312");
- byte[] srcBytes = srcEncode.GetBytes(srcString);//用Unicode去转换源字符串
- byte[] dstBytes = Encoding.Convert(srcEncode, dstEncode, srcBytes);//但是,是从gb2312转到unicode的
- char[] dstChars = new char[e8859Encode.GetCharCount(dstBytes, 0, dstBytes.Length)];
- e8859Encode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);
- return new string(dstChars);
- }
- return srcString;
- }
阅读(2563) | 评论(0) | 转发(0) |