1. 程式人生 > >計算機中的「魔數」(magic number)

計算機中的「魔數」(magic number)

In computer programming, the term magic number has multiple meanings. It could refer to one or more of the following:

  • A constant numerical or text value used to identify a file format or protocol; for files, see List of file signatures
  • Distinctive unique values that are unlikely to be mistaken for other meanings (e.g., 
    Globally Unique Identifiers
    )
  • Unique values with unexplained meaning or multiple occurrences which could (preferably) be replaced with named constants
例如BMP圖片檔案的字首是“BM”,JPEG圖片檔案的字首是“JFIF”,這些檔案的字首就算是Magic number,這是指上面的第一種情況。再如Linux系統使用的uImage壓縮核心的字首是:0x27051956,這個就是uImage的Magic number,屬於上面說的後面兩種情況。三種情況都有共同點,都是用來作為區別事物的,一般用於程式檢測檔案型別做對應的處理工作。