gpt4 book ai didi

c# - 从一个字节中获取值

转载 作者:行者123 更新时间:2023-11-30 22:30:13 25 4
gpt4 key购买 nike

如果我有一个方法接收一个红色字节、一个绿色字节和一个蓝色字节,并且我知道如果我从每个字节中提取红色字节中的最后 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);

}
}

最佳答案

From each byte if i extract the last 3 bits in the red byte, last 2 bits in the green byte and the last 3 bits in the blue byte, how would i accomplish this?

这将屏蔽除您要求的低 n 位之外的所有内容。

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

关于c# - 从一个字节中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9797068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com