假如有这样一个
String str = "abcdefg东afff南b西c北d中e";
在C#中是Unicode编码. 字节数为str.Length * 2
我想知道, 如果这个字串存入到文件中, 在文件中这个字串占多少字节, 应该如何计算?
在文件中是gb2312格式.
StreamWriter sw = new StreamWriter(strFileName, Encoding.GetEncoding("gb2312"));
sw.Write(str); 这时, 这个字串在文件中占多少字节, 应该如何计算, 谢谢!

解决方案 »

  1.   

    直接用MessageBox打出来不就完了?   还要自己一个个数?
    MessageBox.Show(Encoding.GetEncoding("gb2312").GetBytes(str).Length.ToString());
      

  2.   

    System.Text.Encoding.Default.GetBytes(str).Length
      

  3.   

    byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(字符串)//byte[]类型
    b.length就是你要的长度咯
      

  4.   

    System.Text.Encoding.GetEncoding("gb2312").GetBytes(str).Length
      

  5.   

    为gb2312 时
    MessageBox.Show(Encoding.GetEncoding("gb2312").GetBytes(str).Length.ToString());  //输出结果为:  25; 如果 Unicode 的话,MessageBox.Show(Encoding .Unicode.GetBytes (str).Length .ToString ()); //对应结果为40;