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

来源:互联网 发布:python 云词 编辑:程序博客网 时间:2024/06/10 05:55

摘自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,属于上面说的后面两种情况。三种情况都有共同点,都是用来作为区别事物的,一般用于程序检测文件类型做对应的处理工作。
原创粉丝点击