获得字符串字节长度

 public int GetLength(string str)
    {
        if (str.Length == 0) return 0;
        ASCIIEncoding ascii = new ASCIIEncoding();
        int tempLen = 0;
        byte[] s = ascii.GetBytes(str);
        for (int i = 0; i < s.Length; i++)
        {
            if ((int)s[i] == 63) tempLen += 2;
            else tempLen += 1;
        }
        return tempLen;
    }

 

posted @ 2014-11-24 15:27  指尖舞者  阅读(323)  评论(0编辑  收藏  举报