自己试一下吧,会弄清楚很多概念.file="c:\test.txt"
001一二三002
001一二三  002private void button1_Click(object sender, System.EventArgs e)
{ //StreamReader sr = new StreamReader(@"c:\test.txt",System.Text.Encoding.GetEncoding("GB2312"));
//StreamReader sr = new StreamReader(@"c:\test.txt",System.Text.Encoding.Unicode);
StreamReader sr = new StreamReader(@"c:\test.txt",System.Text.Encoding.GetEncoding("GB2312"));
StreamReader sr1 = new StreamReader(@"c:\test.txt",System.Text.Encoding.UTF8);
StreamReader sr2 = new StreamReader(@"c:\test.txt",System.Text.Encoding.Unicode); //System.Windows.Forms.MessageBox.Show(System.Text.Encoding.Default.ToString());
//System.Windows.Forms.MessageBox.Show(System.Text.CodePageEncoding);
string s = sr.ReadToEnd();
string s1 = sr1.ReadToEnd();
string s2 = sr2.ReadToEnd(); System.Windows.Forms.MessageBox.Show(s);
System.Windows.Forms.MessageBox.Show(s1);
System.Windows.Forms.MessageBox.Show(s2);
byte[] ff = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s);
byte[] ff1 = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s1);
byte[] ff2 = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s2); string ff_1 = System.Text.Encoding.GetEncoding("GB2312").GetString(ff,0,3);
string ff_2 = System.Text.Encoding.GetEncoding("GB2312").GetString(ff,3,6);
string ff_3 = System.Text.Encoding.GetEncoding("GB2312").GetString(ff,91,3);

System.Windows.Forms.MessageBox.Show(s);
System.Windows.Forms.MessageBox.Show(s.Substring(0,3));
System.Windows.Forms.MessageBox.Show(s.Substring(3,1));
System.Windows.Forms.MessageBox.Show(s.Substring(4,1)); byte[] t = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s);
string t1 = System.Text.Encoding.GetEncoding("GB2312").GetString(t,0,3);
string t2 = System.Text.Encoding.GetEncoding("GB2312").GetString(t,3,8);
string t3 = System.Text.Encoding.GetEncoding("GB2312").GetString(t,11,3);
//byte[] tt1 = System.Text.Encoding.GetEncoding("GB2312").GetBytes(t,3,8);
//string ss1 = System.Text.Encoding.GetEncoding("GB2312").GetString(tt1);
//System.Windows.Forms.MessageBox.Show(ss1);
//char[] ttt2 = System.Text.Encoding.GetEncoding("GB2312").GetChars(t,11,3);
System.Windows.Forms.MessageBox.Show(s);
sr.Close();}

解决方案 »

  1.   

    典型的定长字符在UNICODE下处理的问题:
    当然可以采用更麻烦但高效一点的办法,就是API使用MEMCOPY.
    在解决过程中弄清楚以下概念:1]WIN2000下的常用的编码方式:
    Ansi [无字头]
    Unicode [FF/FE字头]
    Unicode bigeng [FE/FF字头]
    UTF-8 [FF/FE字头]2)Encoding主要以下四种形式下相互转换.
    GetBytes   String            -->  字节数组
    GetBytes   Unicode 字符数组  -->  字节数组
    GetChars   字节数组   -->  Unicode 字符数组
    GetString   字节数组          -->  String
    --------------------------------------------------
    Char[] = Unicode 字符数组 
    Byte[] = 字节数组
    String = String
    三者之间不能直接互转
    ---------------------------------------------------3)Encoding在流式文件中的含义,注意以下几种情况:
    Encoding.GetEncoding("GB2312");  可用于文件ANSI格式的中西文混排.
    Encoding.GetDefault;             在中文环境下相当于GetEncoding("GB2312")
    Encoding.GetUnicode;             可用于文件UTF16.
    Encoding.GetUTF8;                可用于文件UTF8.
    [忽略此参数];                    相当于Encoding.GetUTF8
    ----------------------------------------------------
    注意事项:
    1:当文件为各类UNICODE格式时,GetEncoding("GB2312")未起作用,自动识别为UNICODE格式,
      估计通过FF/FE(FE/FF)字头,但是识别为具体的UTF格式的机制不清楚.
    2:C#内部处理字符串按UTF16标准,在处理外部文件时按UTF8标准.
    -----------------------------------------------------4)关于楼主问题的关键在于理解Encoding对于哪一方作用的意义,水平有限,比较难描述清楚.
    举例说明:
    StreamReader sr = new StreamReader(@"c:\test.txt",
                      System.Text.Encoding.GetEncoding("GB2312"));
    此时的Encoding是针对流的内容,针对[源]
    byte[] ff = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s);
    此时的Encoding是针对字节数组,针对[目标]
    string ff_1 = System.Text.Encoding.GetEncoding("GB2312").GetString(ff,3,3);
    此时的Encoding是针对字节数组,针对[源]
    ----------------------------------------
    总结:Encoding总是针对非String的一方,现在看来是理所当然的,问题己经解决吧.
    试想:"GB2312"换成不同的内容,会有什么结果,当然有些组合是无意义.5)一些难以描述的东东
      

  2.   

    提供一种思路 string str="fdsa吧宗有1212";
    char[] chs=str.ToCharArray();
    foreach(char ch in chs)
    {
    if((int)ch>256)
    {
    }
    else
    {
    }
    }