Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 以十六进制搜索特定字节_C_Search_Byte_Buffer_Hex - Fatal编程技术网

C 以十六进制搜索特定字节

C 以十六进制搜索特定字节,c,search,byte,buffer,hex,C,Search,Byte,Buffer,Hex,我有一个包含十六进制字节的缓冲区[],我想搜索这个缓冲区以找到特定的字节。例如: 我的缓冲区有4096个字节,如果字节45 34 67 23(一起)在缓冲区内(就像在缓冲区中搜索字符串一样),我想在此进行搜索 你知道我怎么做吗?编程语言是C。只是“蛮力”而已:) haystacklen=4096; 针线=4; foundat=-1; 索引=0;/*如果搜索第二个匹配项,则从其他编号开始*/ 而(指数

我有一个包含十六进制字节的
缓冲区[]
,我想搜索这个缓冲区以找到特定的字节。例如:

我的缓冲区有
4096
个字节,如果字节
45 34 67 23
(一起)在缓冲区内(就像在缓冲区中搜索字符串一样),我想在此进行搜索

你知道我怎么做吗?编程语言是C。

只是“蛮力”而已:)

haystacklen=4096;
针线=4;
foundat=-1;
索引=0;/*如果搜索第二个匹配项,则从其他编号开始*/
而(指数
您也可以使用这个更快的版本。但您必须记住,由于MAKEDWORD宏,这只适用于x86/little-endian处理器

#define MAKEDWORD(a,b,c,d) ((uint32_t) (((uint32_t)a) & 0xFF) | ((((uint32_t)b) & 0xFF) << 8) | ((((uint32_t)c) & 0xFF) << 16) | ((((uint32_t)d) & 0xFF) << 24))
#define NEEDLE (MAKEDWORD(45,34,67,23))

// get the start and end address of the buffer
uint8_t *ptrEndBuffer = ((uint8_t*)buffer) + (4096 - sizeof(NEEDLE));
uint8_t *ptrStartBuffer = (uint8_t*)buffer - 1; // subtract -1 because we also want to get index 0

// while the result is not 0 we are good
while (ptrEndBuffer - ptrStartBuffer) {
    if ((*(uint32_t*)ptrEndBuffer) == NEEDLE) // get an whole integer instead of just one char
        break; // leave the loop if we found a match

    ptrEndBuffer--;
}

// the index will be -1 if we couldn't find a match else we subtract the start address + the 1 we first removed from the end buffer
int index = ((ptrEndBuffer == ptrStartBuffer) ? (-1) : (ptrEndBuffer - (ptrStartBuffer + 1)));

#定义MAKEDWORD(a,b,c,d)((uint32_t)((uint32_t)a)和0xFF)((uint32_t)b和0xFF)什么语言?另外,最天真的实现是否有问题?(即找到值为
45
的第一个字节,查看它后面是否有
34 67 23
,如果没有,重复到数组末尾)很抱歉,我忘了说它是C语言的!听起来不错,也就是说,谢谢:)@Paul也许他的缓冲区包含0x0字节?@RedX:good point-如果GNU/Linux存在
memmemmem
“当有疑问时,使用蛮力”——Ken ThompsonShouldn不是
而(index
?@Paul R:那将是
而不是(您还可以执行以下操作:int index=(int)((int64_t)ptrEndBuffer-((int64_t)ptrStartBuffer+1))//强制转换为long,因为如果在堆栈中工作(高地址),强制转换为int32_t可能会使整数溢出并使其为负
#define MAKEDWORD(a,b,c,d) ((uint32_t) (((uint32_t)a) & 0xFF) | ((((uint32_t)b) & 0xFF) << 8) | ((((uint32_t)c) & 0xFF) << 16) | ((((uint32_t)d) & 0xFF) << 24))
#define NEEDLE (MAKEDWORD(45,34,67,23))

// get the start and end address of the buffer
uint8_t *ptrEndBuffer = ((uint8_t*)buffer) + (4096 - sizeof(NEEDLE));
uint8_t *ptrStartBuffer = (uint8_t*)buffer - 1; // subtract -1 because we also want to get index 0

// while the result is not 0 we are good
while (ptrEndBuffer - ptrStartBuffer) {
    if ((*(uint32_t*)ptrEndBuffer) == NEEDLE) // get an whole integer instead of just one char
        break; // leave the loop if we found a match

    ptrEndBuffer--;
}

// the index will be -1 if we couldn't find a match else we subtract the start address + the 1 we first removed from the end buffer
int index = ((ptrEndBuffer == ptrStartBuffer) ? (-1) : (ptrEndBuffer - (ptrStartBuffer + 1)));