string strVal = "HelloWorld";我想将这个strVal转换成byte[]类型,不知道在C#中怎么弄?

解决方案 »

  1.   

    byte[] buffer = Encoding.ASCII.GetBytes("HelloWorld");
      

  2.   

            string str = "123";
            byte[] b = new byte[str.Length];
            for (int i = 0; i < str.Length; i++) 
            {
                b[i] = Convert.ToByte(str.Substring(i, 1));
            }
    有点笨的方法
      

  3.   

    byte[] p = strVal.getbytes();   
      

  4.   

    byte[]   byVal   = strVal.getbytes();       
      

  5.   

    byte[] b = Encoding.Default.GetBytes(strVal);
    用這個吧
      

  6.   

    strVal.getbytes();    
    有这个方法吗?
    怀疑。
      

  7.   

    string --> byte[]
    byte[] b[] = System.Text.Encoding.ASCII.GetBytes(string);
    -------------------------------------------------------------------------
    string --> byte
    byte b = Convert.ToByte(string);
    -------------------------------------------------------------------------
    byte[] --> string
    string s = Encoding.ASCII.GetString(bytes, 0, nBytesSize);