计算机中的「魔数」(magic number)

摘自wikipedia:https://en.wikipedia.org/wiki/Magic_number_%28programming%29#Examples

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,属于上面说的后面两种情况。三种情况都有共同点,都是用来作为区别事物的,一般用于程序检测文件类型做对应的处理工作。
上一篇:如何在haml中使用内联JavaScript中的Ruby变量?


下一篇:浮点数计算误差