What is the purpose of a N-byte 'magic' number?

470 views Asked by At

When parsing NES roms, the first four bytes are a 'magic' number:

78/0x4E (N)
69/0x45 (E)
83/0x53 (S)
26/0x1A (DOS end of file character)

What purpose does this, or any other examples, provide?

1

There are 1 answers

1
BenW On BEST ANSWER

So-called "magic numbers" are often specified by binary file formats as a kind of "signature" of the file format. Programs that read the file format can check for the magic number and reject the file as invalid if it doesn't match. This provides an easy way to catch garbage data early. It also allows programs that understand multiple formats to figure out what type of file they're reading, so it can be processed appropriately.

In the case of iNES format ROM images, it's no different. The purpose of the magic number in the header is to tell the emulator (or other program) that this is an iNES format image and that it should be parsed as such.