如何将一个位(代表value1)和23位(代表value2)组成uint值?
How to compose uint value from single bit (representing value1) and 23 bits (representing value2)?
我目前逆向工程的一些游戏在 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 att
我找到了一种像这样阅读它们的方法:
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
.
要根据石头(0 或 1)设置位#23 并根据攻击设置位#0...22,请使用按位或运算符:
(stones << 23) | attack
我目前逆向工程的一些游戏在 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 att
我找到了一种像这样阅读它们的方法:
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
.
要根据石头(0 或 1)设置位#23 并根据攻击设置位#0...22,请使用按位或运算符:
(stones << 23) | attack