Lajos Arpad
Lajos Arpad

Reputation: 76464

Is there a declared meaning of the first eight bytes of .png files?

I am reading a code and see the following comment section in it:

            // We need to determine if the image is a PNG or a JPEG
            // PNGs are easier to detect because they have a unique signature (http://www.w3.org/TR/PNG-Structure.html)
            // The first eight bytes of a PNG file always contain the following (decimal) values:
            // 137 80 78 71 13 10 26 10

If I look at the ASCII codes, I get the following text:

ëPNG♪◙→◙

from which PGN is pretty clear. Is there an explanation for the other parts?

Upvotes: 1

Views: 732

Answers (1)

William
William

Reputation: 106

From the PNG specification (https://www.w3.org/TR/PNG-Rationale.html#R.PNG-file-signature)

In ASCII it is: \211 P N G \r \n \032 \n

The first two are to make sure it is recognized as PNG ( a non-ASCII character and the P), the newline afterward is to prevent bad file transfers and the second last (CTRL-Z) character prevents DOS from displaying the file contents and the last newline is for the same reason as the first newline sequence

Upvotes: 3

Related Questions