【发布时间】:2025-12-10 12:30:01
【问题描述】:
想知道是否有一种方法可以一次比较数组中的一组数据。我目前正在从作业中制作一个 JPEG 图像恢复程序,我对 JPEG 字节模式进行了硬编码,它看起来有点乱。
如果我能收集有关如何整理此代码的建议,那真的会对我有所帮助。问题始于 while 循环内的 if 条件。
我要检查的字节模式是 0xff 0xd8 0xff 0xe0 或 0xff 0xd8 0xff 0xe1
#define BLOCKSIZE 512
typedef uint8_t BYTE;
int main(int argc, char* argv[])
{
// create a buffer of 512 Bytes
BYTE buffer[BLOCKSIZE];
// open card, check if fopen is successful
FILE* card = fopen("card.raw", "r");
if (card == NULL)
{
printf("Failed to open card.\n");
return -1;
}
// scan through card, 1 block at a time (512 Bytes) until end of file
while (fread(&buffer, BLOCKSIZE, 1, card) == 1)
{
// read current buffer
fread(&buffer, BLOCKSIZE, 1, card);
// check if current buffer marks the start of a JPEG pattern
if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe0) ||
(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe1))
}
}
【问题讨论】:
-
把它放在一个函数中。将其命名为描述性的名称。