Thomas 的个人资料Blogs日志列表 工具 帮助
1月25日

Encoding and Decoding of code pages

 
Sometimes you'll have to read from a file that has a specific codepage.
Using the FileStream class you can decode the bytes read using the following method:
 

    protected string Read(int size) 
    { 
      byte[] buffer = new byte[size];
      stream.Read(buffer, 0, size); 
      Encoding codepage = Encoding.GetEncoding(437);

      string s;

      if (codepage != null
      { 
        s = codepage.GetString(buffer); 
      } 
      else 
      { 
        s = Encoding.ASCII.GetString(buffer); 
      }

      return s.Trim(); 
    }