无法将XOR结果设置为BitArray类型变量。如何修复此代码?

时间:2015-05-26 13:13:16

标签: c# casting

我试图对一个像素的蓝色字节的两个LSB进行异或,并将结果存储在BitArray变量的索引中,但C#给出了我的转换错误

  

无法隐式转换类型' int'到了布尔'

我知道C#将XOR操作视为int,但我无法修复此代码。知道如何解决这个问题吗?

    Bitmap img = new Bitmap(fname);
    BitArray cipherBits = new BitArray(cipherStringSize * sizeof(Char) * 8);
    int cipherBitCounter = 0;
    Color pixel;
    byte[] cipherByte = new byte[cipherBits.Length/8];

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    if (cipherBitCounter <= cipherBits.Length)
                    {
                        pixel = img.GetPixel(i, j);
                        cipherBits[cipherBitCounter] = ((pixel.B >> 1) & 1) ^ ((pixel.B) & 1); //error here
                        cipherBitCounter++;
                    }
                    else
                    {
                       //do something else
                    }
                }

1 个答案:

答案 0 :(得分:0)

你可能比较1:

cipherBits[cipherBitCounter] =( ((pixel.B >> 1) & 1) ^ ((pixel.B) & 1) ) == 1; //error here

但最好使用Convert.ToBoolean:

https://msdn.microsoft.com/en-us/library/system.convert.toboolean%28v=vs.110%29.aspx

cipherBits[cipherBitCounter] = Convert.ToBoolean( ((pixel.B >> 1) & 1) ^ ((pixel.B) & 1) ); //error here