英文:
My code prints a hex that is not valid for an image, it's not a value from the array that I am comparing. I don't understand where is coming from
问题
以下是您要翻译的代码部分:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// 打开图像文件
FILE *image = fopen("me.jpg", "r");
// 读取图像
// 检查文件是否为NULL
if (image == NULL)
{
return 1;
}
// 存储字符签名在数组中
unsigned char signature[4];
// 存储额外数据
unsigned char extra[16] =
{
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
};
// 读取签名
fread(signature, 4, 1, image);
// 读取额外数据
char extrahex = fread(extra, 16, 1, image);
// 在图像中查找额外数据
for (int i = 0; i < 16; i++)
{
if (extra[i] == extrahex)
{
printf("%#04x\n", extrahex);
return 0;
char hexnum = extrahex;
// JPEG的条件
if (signature[0] == 0xff && signature[1] == 0xd8 && signature[2] == 0xff && signature[3] == hexnum)
{
printf("有效的JPEG格式\n");
}
else
{
printf("不是有效的JPEG格式\n");
}
return 0;
}
}
fclose(image);
}
请注意,您提供的代码存在一些逻辑错误,可能会导致不正确的结果。如果您需要关于代码逻辑方面的帮助,请提出具体的问题。
英文:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// open image
FILE *image = fopen("me.jpg", "r");
// read image
// check if file is NULL
if(image == NULL)
{
return 1;
}
// store char signature in array
unsigned char signature[4];
// store extra
unsigned char extra[16] =
{
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
};
// read signature
fread(signature, 4, 1, image);
// read extra
char extrahex = fread(extra, 16, 1, image);
// find extra in the image
for (int i = 0; i < 16; i++)
{
if(extra[i] == extrahex)
{
printf("%#04x\n", extrahex);
return 0;
char hexnum = extrahex;
// condition for jpeg
if(signature[0] == 0xff && signature[1] == 0xd8 && signature[2] == 0xff && signature[3] == hexnum)
{
printf("valid jpeg format\n");
}
else
{
printf("not a valid jpeg format\n");
}
return 0;
}
}
fclose(image);
}
the return value that is printed is: 0x01, this is not a value from the extra array that I am checking against the image.
And of course, the result is "not a valid jpg format". I tried a different image, it prints the same 0x01 value.
答案1
得分: 0
根据这里提到的内容:https://en.cppreference.com/w/c/io/fread,fread的返回值是函数读取的完整项目的数量。在你的情况下,你尝试读取一个大小为16字节的元素。所以extrahex将始终为0x01。
只是一个猜测:你可能想要使用**char extrahex = signature[3]**而不是。
英文:
As mentioned here: https://en.cppreference.com/w/c/io/fread the return value of fread is the number of full items the function read. In your case you try to read 1 element of 16 bytes in size. So extrahex will always be 0x01.
Just a guess: you might want char extrahex = signature[3] instead
答案2
得分: 0
不需要再调用fread
。相反,您必须检查signature[3]
是否是存储在extra
中的已知值之一。
#include <stdio.h>
int main(void)
{
FILE *image = fopen("me.jpg", "r");
if (!image)
{
return 1;
}
unsigned char signature[4];
unsigned char extra[16] = {
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
};
fread(signature, 4, 1, image);
fclose(image);
if (signature[0] == 0xff && signature[1] == 0xd8 && signature[2] == 0xff)
{
for (size_t i = 0; i < 16; i++)
{
if (signature[3] == extra[i])
{
puts("有效的 JPEG 格式");
return 0;
}
}
}
puts("不是有效的 JPEG 格式");
}
或者,检查signature[3]
的前四位是否为1110
(0xe
)。
#include <stdio.h>
int main(void)
{
FILE *image = fopen("me.jpg", "r");
if (!image)
{
return 1;
}
unsigned char signature[4];
fread(signature, 4, 1, image);
fclose(image);
if (signature[0] == 0xff &&
signature[1] == 0xd8 &&
signature[2] == 0xff &&
(signature[3] >> 4) == 0xe)
{
puts("有效的 JPEG 格式");
}
else
{
puts("不是有效的 JPEG 格式");
}
}
英文:
You do not need to make another call to fread
. Instead, you must check if signature[3]
is one of the known values stored in extra
.
#include <stdio.h>
int main(void)
{
FILE *image = fopen("me.jpg", "r");
if (!image)
{
return 1;
}
unsigned char signature[4];
unsigned char extra[16] = {
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
};
fread(signature, 4, 1, image);
fclose(image);
if (signature[0] == 0xff && signature[1] == 0xd8 && signature[2] == 0xff)
{
for (size_t i = 0; i < 16; i++)
{
if (signature[3] == extra[i])
{
puts("valid jpeg format");
return 0;
}
}
}
puts("not a valid jpeg format");
}
Alternatively, check that the first four bits of signature[3]
are 1110
(0xe
).
#include <stdio.h>
int main(void)
{
FILE *image = fopen("me.jpg", "r");
if (!image)
{
return 1;
}
unsigned char signature[4];
fread(signature, 4, 1, image);
fclose(image);
if (signature[0] == 0xff &&
signature[1] == 0xd8 &&
signature[2] == 0xff &&
(signature[3] >> 4) == 0xe)
{
puts("valid jpeg format");
}
else
{
puts("not a valid jpeg format");
}
}
答案3
得分: 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论