通过偏移量从字节数组中获取 int
Get int from byte array by offset
我是 C++ 新手。无法通过偏移量从字节数组中获取 int。
当我直接从内存中读取时,一切正常,我得到 100 - 这是正确的值
int base = 0x100;
int offset = 0x256;
int easy = memory->ReadMemory<int>(base + offset); // easy = 100
但是如果我尝试获取一大块字节并从中读取,问题就来了
template<class T>
T FromBuffer(uint8_t* buffer, size_t offset)
{
T t_buf = 0;
memcpy(&t_buf, buffer + offset, sizeof(T));
return t_buf;
}
uint8_t* ReadBytes(DWORD Address, int Size)
{
auto arr = new uint8_t[Size];
ReadProcessMemory(TargetProcess, (LPVOID)Address, arr, sizeof(arr), 0);
return arr;
}
auto bytes = memory->ReadBytes(base, 2500);
int hard = *((unsigned int *)&bytes[offset]); // hard = -842150451
uint32_t hard2 = memory->FromBuffer<uint32_t>(bytes, offset); // hard2 = 3452816845
使用 C# 就很容易做到这一点
int hard = BitConverter.ToInt32(bytes, offset);
将这种类型的 C# 代码转换为 C++ 没有任何意义,你被迫在 C# 中做一些古怪的事情,因为做这种类型的操作不是 C# 的目的。
您不需要创建动态缓冲区并执行任何这些操作。只是做:
template <class T>
T RPM(void* addr)
{
T t;
ReadProcessMemory(handle, addr, &t, sizeof(t), nullptr);
return t;
}
int RPM(addr + offset);
我是 C++ 新手。无法通过偏移量从字节数组中获取 int。
当我直接从内存中读取时,一切正常,我得到 100 - 这是正确的值
int base = 0x100;
int offset = 0x256;
int easy = memory->ReadMemory<int>(base + offset); // easy = 100
但是如果我尝试获取一大块字节并从中读取,问题就来了
template<class T>
T FromBuffer(uint8_t* buffer, size_t offset)
{
T t_buf = 0;
memcpy(&t_buf, buffer + offset, sizeof(T));
return t_buf;
}
uint8_t* ReadBytes(DWORD Address, int Size)
{
auto arr = new uint8_t[Size];
ReadProcessMemory(TargetProcess, (LPVOID)Address, arr, sizeof(arr), 0);
return arr;
}
auto bytes = memory->ReadBytes(base, 2500);
int hard = *((unsigned int *)&bytes[offset]); // hard = -842150451
uint32_t hard2 = memory->FromBuffer<uint32_t>(bytes, offset); // hard2 = 3452816845
使用 C# 就很容易做到这一点
int hard = BitConverter.ToInt32(bytes, offset);
将这种类型的 C# 代码转换为 C++ 没有任何意义,你被迫在 C# 中做一些古怪的事情,因为做这种类型的操作不是 C# 的目的。
您不需要创建动态缓冲区并执行任何这些操作。只是做:
template <class T>
T RPM(void* addr)
{
T t;
ReadProcessMemory(handle, addr, &t, sizeof(t), nullptr);
return t;
}
int RPM(addr + offset);