1、查找字符串编码方式
string cTxt=dt.Rows[0][4].ToString();//乱码字符 foreach (EncodingInfo ei in Encoding.GetEncodings()) { Byte[] mybyte = System.Text.Encoding.GetEncoding(ei.CodePage).GetBytes(cTxt); sb.Append(ei.Name + "(" + ei.CodePage + "):" + System.Text.Encoding.GetEncoding("gb2312").GetString(mybyte, 0, mybyte.Length) + "\r\n"); } Console.WriteLine(sb.ToString()); //用系统中的所有编码方式,进行字符串解析,找到中文显示正常的编码方式
2、改变字符串编码方法
1)sybase数据CP850转成GB2312
public static class CharConvert { public static string CP850ToGB2312(string str) { try { Encoding cp850 = Encoding.GetEncoding(850); Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936 byte[] temp = cp850.GetBytes(str); return Encoding.GetEncoding("gb2312").GetString(temp, 0, temp.Length); } catch (Exception ex)//(UnsupportedEncodingException ex) { Console.Write(ex.Message); return null; } } }
2) 指定源编码方式转成ToGB2312
public static class CharConvert { public static string ToGB2312(int codepage, string str) { try { Encoding cp850 = Encoding.GetEncoding(codepage); Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936 byte[] temp = cp850.GetBytes(str); return Encoding.GetEncoding("gb2312").GetString(temp, 0, temp.Length); } catch (Exception ex)//(UnsupportedEncodingException ex) { Console.Write(ex.Message); return null; } } }