我正在尝试在一块内存中搜索一个4字节的十六进制值(0xAABBAABB),然后将之前的4个字节复制到一个单独的变量中。
0xAABBAABB是消息终止符,我在此之后的4个字节之后。
我从* uint8_t和消息大小开始给出内存中数据的位置。所以* uint8保存了消息的前2个字节。
任何帮助将不胜感激。
由于
答案 0 :(得分:1)
尝试这样的事情:
interface ArrayLike<T> {
length: number;
[n: number]: T;
}
class Point implements ArrayLike<number> {
[0]: number = 10;
length: number = 1;
}
或者这个:
uint8_t *msg = ...;
int msgsize = ...;
...
uint8_t bytes[4];
bool found = false;
msg += 4;
msgsize -= 4;
while (msgsize >= 4)
{
if (*(uint32_t*)msg == 0xAABBAABB)
{
memcpy(bytes, msg-4, 4);
found = true;
break;
}
++msg;
--msgsize;
}
答案 1 :(得分:0)
我不会为你编写完整的代码,但如果你能读取2个字节,那么读取4个字节将非常简单。只需读取2个字节,移位并再次读取2个字节。假设readUInt8();
从您的消息返回2个字节。
std::uint16_t readUInt16()
{
const std::uint16_t firstByte = readUInt8();
const std::uint16_t secondByte = readUInt8();
std::uint16_t value = 0;
value |= firstByte << 8;
value |= secondByte;
return value;
}
然后检查readUInt16() == 0xAABBAABBu
是否为前4个字节。请记住检查邮件大小是否与4个字节对齐。