Index: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Home > Magic number (programming)
In computer programming, a magic number is a special constant used for some specific purpose. It is called magic because its value or presence is inexplicable without some additional knowledge.Magic numbers are often chosen based on (among others):
- ASCII code (most common)
- Representation in hexadecimal (e.g. decimal 305419896 is hexadecimal
0x12345678)
- Sometimes hexspeak is used
1 Magic numbers in files
An early convention in the Unix operating system was that ( binary) files started with two bytes containing a "magic number" identifying the type of the file. These were originally used by the Unix linker and loader. The concept has been expanded on over time, and is now in current use by many other programs across many operating systems. In a quick hack, the very earliest magic numbers were PDP-11 branch instruction s. The concept of magic numbers can be generalised to all files, since any unencoded binary data is essentially a number; most file formats can thus be identified by some signature that occurs somewhere in the file. Detecting such sequences is therefore an effective way of distinguishing between file formats - and can often yield further information at the same time.
Some examples:
- Compiled Java class files ( bytecode) start with
0xCAFEBABE.
- GIFGIF (Graphics Interchange Format) is a bitmap image format that is widely used on the World Wide Web, both for still images and for animations. GIF" is often pronounced giff with a hard g (that is, like "gift" without the final t), but the correct pronunc image files have the ASCII code for 'GIF89a' (
0x474946383961) or 'GIF87a' (0x474946383761)
- JPEGIn computing, JPEG is a commonly used standard method of compressing photographic images. The file format which employs this compression is commonly also called JPEG; platforms with short file extensions may use. JPE to identify this format. The name stan image files have the ASCII code for 'JFIF' (
0x4A464946) followed by more metadataMetadata is data about data. An example is a library catalog card, which contains data about the nature and location of a book: It is data about the data in the book referred to by the card. The content combined with its metadata is often called a content about the file.
- PNGPNG Portable Network Graphics , sometimes pronounced as ping , is a relatively new bitmap image format that is becoming popular on the World Wide Web and elsewhere. PNG was largely developed to deal with some of the shortcomings of the GIF format and allo image files begin with an 8- byte signature which identifies the file as a PNG file and allows immediate detection of some common file-transfer problems:
\211 P N G \r \n \032 \n (0x89504e470d0a1a0a)
- Standard MIDI music files have the ASCII code for 'MThd' (
0x4D546864) followed by more metadata about the file.
- Unix script files usually start with a shebang, #! (
0x2321) followed by the path to an interpreter.
- Old MS-DOS LE (Linear Executable) .exe files and the newer Microsoft Windows PE (Portable Executable) .exe files start with the ASCII string 'MZ', the initials of the designer of the file format. (
0x4D5A)
- Executables for the Game Boy and Game Boy Advance handheld video game systems have a 48-byte or 156-byte magic number, respectively, at a fixed spot in the header. This magic number encodes a bitmap of the Nintendo logo.
The Unix command file can read and interpret magic numbers from files.
Read more »