从一个字节获取值

时间:2012-03-21 00:37:25

标签: c# winforms byte bit-manipulation

如果我有一个方法,它接收一个字节的红色,一个字节的绿色和一个字节的蓝色,我知道从每个字节,如果我提取红色字节中的最后3位,最后2位在绿色字节和蓝色字节中的最后3位,我将如何实现这一目标?我正在使用下面的代码,但它并不完全正常。我在这做错了什么?

    private const byte InverseBlueMask = 7; // 00000111
    private const byte InverseGreenMask = 3; // 00000011
    private const byte InverseRedMask = 7; //  00000111

    //private const byte InverseBValueMask = 31; // 00011111
    //private const byte InverseGValueMask = 231; // 11100111
    //private const byte InverseRValueMask = 248; //  11111000



  public void getEachBitOfMessage(byte byteToManipulate, int colour)
    {
        byte value = 0;
        byte returnByte = 0; 


        if (colour == BLUE)
        {
            value = (byte)(byteToManipulate | BValueMask);
            value = (byte)(value >> 5);
            returnByte = (byte)(byteToManipulate | InverseBlueMask);
            returnByte = (byte)(returnByte & value);
            String theByte = returnByte.ToString(); 

        }
        else if (colour == GREEN)
        {
            value = (byte)(byteToManipulate | GValueMask);
            value = (byte)(value >> 3);
            returnByte = (byte)(byteToManipulate | InverseGreenMask);
            returnByte = (byte)(returnByte & value);
            String theByte = returnByte.ToString(); 

        }
        else if (colour == RED)
        {
            value = (byte)(byteToManipulate | RValueMask);
            returnByte = (byte)(byteToManipulate | InverseRedMask);
            returnByte = (byte)(returnByte & value);
            String theByte = returnByte.ToString(); 

        }
     }

这是我用来将消息的比特放入每个颜色字节的方法。

    private const byte BlueMask = 248; // 11111000
    private const byte GreenMask = 252; // 11111100
    private const byte RedMask = 248; //  11111000
    private const byte BValueMask = 224; // 11100000
    private const byte GValueMask = 24; // 00011000
    private const byte RValueMask = 7; //  00000111



     public byte changeEachBit(byte byteToManipulate, int colour, byte theMessage)
     {

        byte value = 0;
        byte returnByte = 0; 

        if (colour == BLUE)
        {
           value= (byte)(theMessage & BValueMask);
           value = (byte)(value >> 5); 
           returnByte = (byte)(byteToManipulate & BlueMask);
           returnByte = (byte)(returnByte | value); 

        }
        else if (colour == GREEN)
        {
            value = (byte)(theMessage & GValueMask);
            value = (byte)(value >> 3);
            returnByte = (byte)(byteToManipulate & GreenMask);
            returnByte = (byte)(returnByte | value);

        }
        else if (colour == RED)
        {
            value = (byte)(theMessage & RValueMask);
            returnByte = (byte)(byteToManipulate & RedMask);
            returnByte = (byte)(returnByte | value);

        }
     }

3 个答案:

答案 0 :(得分:2)

  

如果我提取红色字节中的最后3位,则从每个字节开始,最后2位   绿色字节中的位和蓝色字节中的最后3位,如何   我做到了吗?

除了你要求的低n位之外,这将掩盖所有内容。

red &= 0x07;
green &= 0x03;
blue &= 0x07;

答案 1 :(得分:1)

这有效..

    private const byte InverseBlueMask = 7; // 00000111
    private const byte InverseGreenMask = 3; // 00000011
    private const byte InverseRedMask = 7; //  00000111

    public void getEachBitOfMessage(byte byteToManipulate, int colour)
    {
        byte value = 0;

        if (countToByte == 3)
        {
            byte blueAreaInTotal = 0;
            byte greenAreaInTotal = 0;
            byte redAreaInTotal = 0;
            byte total = 0; 

            redAreaInTotal = (byte)(redCount);
            blueAreaInTotal = (byte)(blueCount << 5);
            greenAreaInTotal = (byte)(greenCount << 3);

            total = (byte)(total | redAreaInTotal); 
            total = (byte)(total | blueAreaInTotal);
            total = (byte)(total | greenAreaInTotal); 
            convertToChar(total);

            redCount = 0;
            blueCount = 0;
            greenCount = 0; 
            countToByte = 0; 
        }

        if (colour == BLUE)
        {
            value = (byte)(byteToManipulate & InverseBlueMask);
            blueCount = value; 
        }
        else if (colour == GREEN)
        {
            value = (byte)(byteToManipulate & InverseGreenMask);
            greenCount = value; 

        }
        else if (colour == RED)
        {
            value = (byte)(byteToManipulate & InverseRedMask);
            redCount = value; 
        }

        countToByte++; 
    }

答案 2 :(得分:0)

private const byte BValueMask = 224; // 11100000

/* ... */

    if (colour == BLUE)
    {
        value = (byte)(byteToManipulate | BValueMask);
        value = (byte)(value >> 5);

这里的效果是打开高三位,然后丢弃低五位。你也可以写一下:value = 7;。也许这三组语句旨在使用不同的掩码或打算使用不同的位操作?