编码总结

对于URL编码:
可以用:HttpUtility.UrlEncode(s,Encoding.GetEncoding("GB2312"));得到gb2312编码。如:
 Stream   stream=Request.InputStream;  
  StreamReader   sr=new   StreamReader(stream,Encoding.UTF8);  
  string   ss=sr.ReadToEnd();   
   byte[]   bs=Encoding.UTF8.GetBytes(ss);  
  string   cc=HttpUtility.UrlDecode(bs,0,bs.Length,Encoding.GetEncoding("GB2312"));
对于文件中读写数据:
用StreamWriter,可以指定文件的编码,要是gb2312就用(默认是utf-8)
StreamWriter sw = new StreamWriter(fileName, false, System.Text.Encoding.GetEncoding(936));
用FileStream读入一个byte[],然后调用
Encoding.GetEncoding("gb2312").GetString()
private string ToGB2312(string utfInfo)
  {
   string gb2312Info = string.Empty;
   Encoding utf8 = Encoding.UTF8;
   Encoding gb2312 = Encoding.GetEncoding("gb2312");
   byte[] unicodeBytes = utf8.GetBytes(utfInfo);
   byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);
   char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
   gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
   string gb2312info = new string(asciiChars);
   return gb2312info;
  }

你可能感兴趣的:(编码)