C#无法将字符串转换为字节数组到期望的结果?

Dro*_*one 2 c# string bytearray

我有一个只存储1和0的字符串..现在我需要将它转换为字节数组.我试过了 ..

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                        byte[] d = encoding.GetBytes(str5[1]);
Run Code Online (Sandbox Code Playgroud)

但它给我字节数组的ASCII,如48和49,但我想在我的字节数组中有1和0 ..可以任何一个帮助

Mar*_*ell 5

这是编码的正确结果.编码产生字节,而不是.如果需要,则使用逐位运算符检查每个字节.即

foreach(var byte in d) {
    Console.WriteLine(byte & 1);
    Console.WriteLine(byte & 2);
    Console.WriteLine(byte & 4);
    Console.WriteLine(byte & 8);
    Console.WriteLine(byte & 16);
    Console.WriteLine(byte & 32);
    Console.WriteLine(byte & 64);
    Console.WriteLine(byte & 128);
}
Run Code Online (Sandbox Code Playgroud)