using System;namespace CSDN_Exp2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string str="xxxxyyyyyxyyx";   //想多少个就多少个
Class1 myclass=new Class1();
myclass.StrAppend(str); }
public  void StrAppend(string strsource)
{
int k=strsource.Length;
char[] target=new char[k];
for(int i=0;i<k;i++)
{
if(strsource[i]=='x' || strsource[i]=='X')
{
target[i]='0';
}
if(strsource[i]=='y' || strsource[i]=='Y')
{
target[i]='1';
}
}
Console.Write(target);
Console.WriteLine();
}
}
}

解决方案 »

  1.   

    可以用位运算符,^  |  &
      

  2.   

    .net有一个BitArray类,楼主参考一下,搞得好就不用自己写了
      

  3.   

    int[] bits = new int[]{1,0,1,0,1,0,1,0};byte bit = 0;for(int i = 0; i < bits.Length; i++){
        bit |= (byte)(bits[i] << (7 - i));   // 将bits[i] 左移7位, 
                                             // i = 0 时bits[i] << (7 - i)等于 10000000
    }MessageBox.Show(bit.ToString());        // bit = 170
      

  4.   

    可以先合并成字符串,再用Convert.ToByte(String, 2)转换成Byte