Reading Hexadecimal Bytes

One hexadecimal digit describes four bits, so two such digits can write the value of one byte.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Decode a byte
  2. Preserve width at the output
  3. Decode each digit as four positions
  4. Keep digit order separate from order of whole bytes

Decode a byte

The digit A represents ten. The byte written A3 is \(10\cdot16+3=163\). In binary it is 10100011. See binary position values for how those groups contribute.

Preserve width at the output

The byte for numeric value five can be written 05 in a hex dump to keep each group's width visible. A variable-width spelling needs delimiters to avoid confusing adjacent values. Parsing and formatting explains why the serialized width is part of the format.[1]

Decode each digit as four positions

A hexadecimal byte written 7a has high nibble seven and low nibble ten. Its decimal value is \(7\cdot16+10=122\), and its two four-bit groups are 0111 1010. Grouping bits into nibbles avoids recomputing all eight powers separately. See writing binary integers.

07 → 0000 0111 → 7
7a → 0111 1010 → 122
a7 → 1010 0111 → 167

Keep digit order separate from order of whole bytes

Within a printed hexadecimal byte the left digit is the high nibble. Endianness applies when several bytes represent one integer under a format. Test input with a leading zero and multiple distinct bytes. See reading byte order.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩