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

huangapple go评论115阅读模式
英文:

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

问题

以下是您要翻译的代码部分:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. // 打开图像文件
  7. FILE *image = fopen("me.jpg", "r");
  8. // 读取图像
  9. // 检查文件是否为NULL
  10. if (image == NULL)
  11. {
  12. return 1;
  13. }
  14. // 存储字符签名在数组中
  15. unsigned char signature[4];
  16. // 存储额外数据
  17. unsigned char extra[16] =
  18. {
  19. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  20. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
  21. };
  22. // 读取签名
  23. fread(signature, 4, 1, image);
  24. // 读取额外数据
  25. char extrahex = fread(extra, 16, 1, image);
  26. // 在图像中查找额外数据
  27. for (int i = 0; i < 16; i++)
  28. {
  29. if (extra[i] == extrahex)
  30. {
  31. printf("%#04x\n", extrahex);
  32. return 0;
  33. char hexnum = extrahex;
  34. // JPEG的条件
  35. if (signature[0] == 0xff && signature[1] == 0xd8 && signature[2] == 0xff && signature[3] == hexnum)
  36. {
  37. printf("有效的JPEG格式\n");
  38. }
  39. else
  40. {
  41. printf("不是有效的JPEG格式\n");
  42. }
  43. return 0;
  44. }
  45. }
  46. fclose(image);
  47. }

请注意,您提供的代码存在一些逻辑错误,可能会导致不正确的结果。如果您需要关于代码逻辑方面的帮助,请提出具体的问题。

英文:
  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. #include &lt;string.h&gt;
  4. int main()
  5. {
  6. // open image
  7. FILE *image = fopen(&quot;me.jpg&quot;, &quot;r&quot;);
  8. // read image
  9. // check if file is NULL
  10. if(image == NULL)
  11. {
  12. return 1;
  13. }
  14. // store char signature in array
  15. unsigned char signature[4];
  16. // store extra
  17. unsigned char extra[16] =
  18. {
  19. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  20. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
  21. };
  22. // read signature
  23. fread(signature, 4, 1, image);
  24. // read extra
  25. char extrahex = fread(extra, 16, 1, image);
  26. // find extra in the image
  27. for (int i = 0; i &lt; 16; i++)
  28. {
  29. if(extra[i] == extrahex)
  30. {
  31. printf(&quot;%#04x\n&quot;, extrahex);
  32. return 0;
  33. char hexnum = extrahex;
  34. // condition for jpeg
  35. if(signature[0] == 0xff &amp;&amp; signature[1] == 0xd8 &amp;&amp; signature[2] == 0xff &amp;&amp; signature[3] == hexnum)
  36. {
  37. printf(&quot;valid jpeg format\n&quot;);
  38. }
  39. else
  40. {
  41. printf(&quot;not a valid jpeg format\n&quot;);
  42. }
  43. return 0;
  44. }
  45. }
  46. fclose(image);
  47. }

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.

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

答案1

得分: 0

根据这里提到的内容:https://en.cppreference.com/w/c/io/freadfread的返回值是函数读取的完整项目的数量。在你的情况下,你尝试读取一个大小为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中的已知值之一。

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. FILE *image = fopen("me.jpg", "r");
  5. if (!image)
  6. {
  7. return 1;
  8. }
  9. unsigned char signature[4];
  10. unsigned char extra[16] = {
  11. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  12. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
  13. };
  14. fread(signature, 4, 1, image);
  15. fclose(image);
  16. if (signature[0] == 0xff && signature[1] == 0xd8 && signature[2] == 0xff)
  17. {
  18. for (size_t i = 0; i < 16; i++)
  19. {
  20. if (signature[3] == extra[i])
  21. {
  22. puts("有效的 JPEG 格式");
  23. return 0;
  24. }
  25. }
  26. }
  27. puts("不是有效的 JPEG 格式");
  28. }

或者,检查signature[3]的前四位是否为11100xe)。

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. FILE *image = fopen("me.jpg", "r");
  5. if (!image)
  6. {
  7. return 1;
  8. }
  9. unsigned char signature[4];
  10. fread(signature, 4, 1, image);
  11. fclose(image);
  12. if (signature[0] == 0xff &&
  13. signature[1] == 0xd8 &&
  14. signature[2] == 0xff &&
  15. (signature[3] >> 4) == 0xe)
  16. {
  17. puts("有效的 JPEG 格式");
  18. }
  19. else
  20. {
  21. puts("不是有效的 JPEG 格式");
  22. }
  23. }
英文:

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.

  1. #include &lt;stdio.h&gt;
  2. int main(void)
  3. {
  4. FILE *image = fopen(&quot;me.jpg&quot;, &quot;r&quot;);
  5. if (!image)
  6. {
  7. return 1;
  8. }
  9. unsigned char signature[4];
  10. unsigned char extra[16] = {
  11. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  12. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
  13. };
  14. fread(signature, 4, 1, image);
  15. fclose(image);
  16. if (signature[0] == 0xff &amp;&amp; signature[1] == 0xd8 &amp;&amp; signature[2] == 0xff)
  17. {
  18. for (size_t i = 0; i &lt; 16; i++)
  19. {
  20. if (signature[3] == extra[i])
  21. {
  22. puts(&quot;valid jpeg format&quot;);
  23. return 0;
  24. }
  25. }
  26. }
  27. puts(&quot;not a valid jpeg format&quot;);
  28. }

Alternatively, check that the first four bits of signature[3] are 1110 (0xe).

  1. #include &lt;stdio.h&gt;
  2. int main(void)
  3. {
  4. FILE *image = fopen(&quot;me.jpg&quot;, &quot;r&quot;);
  5. if (!image)
  6. {
  7. return 1;
  8. }
  9. unsigned char signature[4];
  10. fread(signature, 4, 1, image);
  11. fclose(image);
  12. if (signature[0] == 0xff &amp;&amp;
  13. signature[1] == 0xd8 &amp;&amp;
  14. signature[2] == 0xff &amp;&amp;
  15. (signature[3] &gt;&gt; 4) == 0xe)
  16. {
  17. puts(&quot;valid jpeg format&quot;);
  18. }
  19. else
  20. {
  21. puts(&quot;not a valid jpeg format&quot;);
  22. }
  23. }

答案3

得分: 0

用Oka的示例解决了。有趣的是,我使用地址和指针进行了操作,但在printf中仍然得到了1作为返回值。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

英文:

Solved with the example of Oka. Interesting though, I played with addresses and pointers and still got 1 as return in printf.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

huangapple
  • 本文由 发表于 2023年7月12日 20:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定