各位:
  何将字符串“010101” 转换为二进制数组byte 010101,谢谢!
    

解决方案 »

  1.   

    string s = "010101"; 
    BinaryWriter.write(s); 
      

  2.   

    老大,我想把结果放在一个Byte[]数组里面
    BinaryWriter.write(s); 返回的是一个Byte[]数组,还是直接写在文件里了,谢谢!
      

  3.   


    string str = "010101";
    byte[] b = System.Text.Encoding.Default.GetBytes(str);b中得到的是"0"的ascii码
      

  4.   

    不是的,是把“010101” 转换成BYTE数组,BYTE数组内容为010101,谢谢!
      

  5.   

    var s = "010101";
                byte[] output = new byte[s.Length];
                for (int i = 0; i < s.Length; i++)
                {
                    output[i] = byte.Parse( s[i].ToString());
                }如果是将"010101"的二进制值变成byte数组,就不一样了
     var s = "010101";
                long num = Convert.ToInt64(s, 2);
                var output = BitConverter.GetBytes(num);
      

  6.   

            static void Main(string[] args)
            {
                string s = "11111111";
                byte b = Convert.ToByte(s, 2);//最后一个参数指定进制
                Console.WriteLine(b);
            }
    输出255,注意使用的时候要Catch错误,防止S字符串的值超过255啊
      

  7.   


    没看清需求,再来一个:
    static void Main(string[] args)
            {
                string s = "11101111";
                byte[] bs = new byte[s.Length];
                for (int i = 0; i < s.Length; i++)
                {
                    bs[i] = Convert.ToByte(s[i].ToString(), 2);
                }
                foreach (byte b in bs)
                    Console.WriteLine(b);
            }
    输出:1 1 1 0 1 1 1 1 
      

  8.   

    static void Main(string[] args)
            {
                string s = "010101";            byte[] output = new byte[s.Length];
                for (int i = 0; i < s.Length; i++)
                {
                    output[i] = byte.Parse(s[i].ToString());
                }
                foreach (var item in output)
                {
                    Console.Write(item);
                }
     
                Console.Read();
            }