Most file formats have a βMagic Numberβ at the beginning as a pair of bytes that indicates which file it has.
GIF begins with "GIF89a" or "GIF87a", that is, bytes 47.49.46.38.39.61 or 47.49.46.38.37.61 (both hexadecimal)
JPEG starts with FF, D8 and ends with FF, D9
PNG starts at 89.50.4E, 47.0D, 0A, 1A, 0A
Learn more about magic numbers on Wikipedia.
In other formats, you can specify a magic number. Some do not.
source share