有好几种方式建议byte[] mybyte=System.Text.Encoding.Default.GetBytes(str);mybyte.Length

解决方案 »

  1.   

    还可以 public double CnLen(string str)
    {
    ASCIIEncoding n=new ASCIIEncoding();
    byte [] mybyte=n.GetBytes(str);
    double len=str.Length;
    for(int i=0;i<str.Length;i++)
    {
    if(mybyte[i]==63)
    {
    len=len+1;
    }
    }
    return len/2;
    // int temp;
    // double len=str.Length;
    // for(int i=0;i<str.Length;i++)
    // {
    // temp=(int)str[i];
    // if(temp<0)
    // {
    // temp+=65536;
    // }
    // if(temp>255)
    // {
    // len+=1;
    // }
    // }
    // return len/2;
    }
      

  2.   

    同意xhan2000。因为在.NET中都是才有Unicode,所以汉字和字符都占2个字节。建议采用第一种方式,即优雅,又简洁。private int GetBytes(String str)
    {
      byte[] mybyte=System.Text.Encoding.Default.GetBytes(str);
      return mybyte.Length;
    }------------------
    Think and Thank