CodeSOD: Magical Bytes Note

CodeSOD: Magical Bytes

"Magic bytes" are a common part of a file header, helping identify the type of file. For example, a bitmap file starts with "BM" and a PGM file starts with "PN". Christer's team was working on software to manipulate CAD file types and needed to identify when a file was a CAD file but not a specific UFF file type. They needed to check that the file did not start with 0xabb0, 0xabb1, or 0xabb3. A coworker wrote a validation check but confused hexadecimal numbers with strings, leading to incorrect comparisons. This mistake created a new, nonsensical numbering system where 0xABB0 and 0xabb0 were considered different. This code sample raises questions about the relationship between uppercase and lowercase hexadecimal numbers, such as whether 0xABB0 < 0xABB1 implies 0xABB0 < 0xabb1. The code also raises the question of how to write a lowercase 3 in hexadecimal.