从字节中获取值
本文关键字:获取 字节 | 更新日期: 2023-09-27 18:28:39
如果我有一个方法,它接收一个红色字节、一个绿色字节和一个蓝色字节,并且我知道从每个字节中提取红色字节中的最后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位,最后2位绿色字节中的位和蓝色字节中的最后3位,如何我做到了吗?
这将屏蔽除您要求的较低n位之外的所有位。
red &= 0x07;
green &= 0x03;
blue &= 0x07;
这很有效。。
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++;
}
private const byte BValueMask = 224; // 11100000
/* ... */
if (colour == BLUE)
{
value = (byte)(byteToManipulate | BValueMask);
value = (byte)(value >> 5);
这里的效果是打开上面的三个比特,然后扔掉下面的五个比特。你不妨写下:value = 7;
。也许这三组语句的目的是使用不同的掩码,或者使用不同的位操作?