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评论70阅读模式
英文:

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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

int main() 
{
    // open image
    FILE *image = fopen(&quot;me.jpg&quot;, &quot;r&quot;);
    // 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 &lt; 16; i++) 
    {
        if(extra[i] == extrahex)
        {
            printf(&quot;%#04x\n&quot;, extrahex);
            return 0;
            char hexnum = extrahex;
            // condition for jpeg
            if(signature[0] == 0xff &amp;&amp; signature[1] == 0xd8 &amp;&amp; signature[2] == 0xff &amp;&amp; signature[3] == hexnum) 
            {
                printf(&quot;valid jpeg format\n&quot;);
            } 
            else 
            {
                printf(&quot;not a valid jpeg format\n&quot;);
            }
            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.

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中的已知值之一。

#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]的前四位是否为11100xe)。

#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 &lt;stdio.h&gt;

int main(void)
{
    FILE *image = fopen(&quot;me.jpg&quot;, &quot;r&quot;);

    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 &amp;&amp; signature[1] == 0xd8 &amp;&amp; signature[2] == 0xff)
    {
        for (size_t i = 0; i &lt; 16; i++)
        {
            if (signature[3] == extra[i])
            {
                puts(&quot;valid jpeg format&quot;);
                return 0;
            }
        }
    }

    puts(&quot;not a valid jpeg format&quot;);
}

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

#include &lt;stdio.h&gt;

int main(void)
{
    FILE *image = fopen(&quot;me.jpg&quot;, &quot;r&quot;);

    if (!image)
    {
        return 1;
    }

    unsigned char signature[4];

    fread(signature, 4, 1, image);
    fclose(image);

    if (signature[0] == 0xff &amp;&amp;
        signature[1] == 0xd8 &amp;&amp;
        signature[2] == 0xff &amp;&amp;
        (signature[3] &gt;&gt; 4) == 0xe)
    {
        puts(&quot;valid jpeg format&quot;);
    }
    else
    {
        puts(&quot;not a valid jpeg format&quot;);
    }
}

答案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:

确定