Binary Files · 14 / 14 Exercise Medium

Recognize the type of a file

Objective

The extension of a file name can lie; the content cannot. Many formats start with fixed bytes (a magic number) that identify them.

Instructions

  • The first line has the number n of files; then one file name per line (they exist in the working folder).
  • Read the first 8 bytes of each one and show name: type, where the type is PNG (89 50 4E 47 0D 0A 1A 0A), GIF ("GIF8"), PDF ("%PDF"), ZIP ("PK\x03\x04"), JPEG (FF D8 FF) or unknown if it matches none.
Input
2
foto.dat
doc.txt
Output
foto.dat: PNG
doc.txt: unknown

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise