【发布时间】:2020-08-18 14:57:23
【问题描述】:
我目前对某些游戏进行逆向工程,将两个值存储在 3 个字节中。
first value 大小是 1 bit 和 second value 大小是 23 bits。
[0][000 0001 0001 0111 1001 0000] 0 石头,71568 att
[1][000 0000 0010 1001 0000 0111] 1 石,10511 攻击力
我找到了这样的阅读方式:
public byte AddedStonesCount
{
get
{
byte[] att_plus_stones_count_as_bytes = new byte[4];
Buffer.BlockCopy(this.Data, 21, att_plus_stones_count_as_bytes, 0, 3);
uint att_plus_stones_count = BitConverter.ToUInt32(att_plus_stones_count_as_bytes, 0);
return (byte)(att_plus_stones_count >> 23);
}
set
{
}
}
public uint Attack
{
get
{
byte[] att_plus_stones_count_as_bytes = new byte[4];
Buffer.BlockCopy(this.Data, 21, att_plus_stones_count_as_bytes, 0, 3);
uint att_plus_stones_count = BitConverter.ToUInt32(att_plus_stones_count_as_bytes, 0);
return att_plus_stones_count << 9 >> 9;
}
set
{
}
}
但我还需要一种方法来改变它们。这就是我总是出错的地方。
我尝试使用att & 0x800000 来获得1 0000 0000 0000 0000 0000 添加攻击,但奇怪的是它给了我 0 而不是我期望的值。
我在这里做了一些测试:
uint test = 0x80290F; //8399119 - here we store 1 stone added and 10511 attack power
uint test_composition = 10511 & (0x800000); //this does not give me 8399119 :/
BitArray ba = new BitArray(BitConverter.GetBytes(test));
string test_original = string.Empty;
for (int i = 0; i < ba.Length; i++)
test_original += ba.Get(i) ? 1 : 0;
uint att = test << 9 >> 9;
BitArray ba_att = new BitArray(BitConverter.GetBytes(att));
string test_att = string.Empty;
for (int i = 0; i < ba_att.Length; i++)
test_att += ba_att.Get(i) ? 1 : 0;
uint stones = test >> 23;
帮助...
例如,当宠物攻击=10511,加石数量=1,那么最终值应该是8399119。
【问题讨论】:
-
您要达到的最终价值是多少?我不知道什么是石头或宠物攻击(
att和pet attack一样吗?)。我认为如果您只关注位操作的问题并与您的术语保持一致,它将使这个问题更加清晰。 -
@itsme86 - 我把例子放在我的问题的最后。
-
哦,我明白了。看起来您应该只使用 OR (
|) 而不是 AND (&)。 -
(stones << 23) | attack? -
@KlausGütter - 谢谢,这行得通。把它作为答案,所以我可以接受。
标签: c# bitwise-operators