Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

Bits, Bytes and Hex for Examiners

Learn the number systems forensic examiners use every day: binary, hexadecimal, byte order (endianness), ASCII and Unicode text, and how to read a hex dump with Python.

A forensic tool shows you files and folders, but underneath everything is bytes. When a tool cannot parse a damaged file system, or when you want to confirm what it told you, you read the bytes yourself. A hex editor or hex dump shows each byte as two hexadecimal digits, with the matching text beside it. This lesson teaches you to read one fluently and to decode the numbers stored inside, including the byte-order trap that confuses almost every beginner.

Binary and hexadecimal

A bit is a 0 or 1. A byte is 8 bits and holds a value from 0 to 255. Writing bytes in binary is long (11111111), so examiners use hexadecimal (base 16), where one digit covers 4 bits and two digits cover one byte.

Decimal Binary Hex
0 0000 0
9 1001 9
10 1010 A
15 1111 F
255 11111111 FF

Hex values are often written 0x1F or 1Fh. The program below converts between systems.

Python Convert between decimal, binary and hex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading a hex dump

A classic dump has three columns: the offset (position from the start), the bytes in hex, and the printable ASCII. Dots stand for unprintable bytes. Here is a dump of a PNG file header (sample, typical layout):

TEXT
00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 0d 49 48 44 52  |.PNG........IHDR|
00000010  00 00 01 00 00 00 00 c8  08 06 00 00 00 ab cd ef  |................|

The first four bytes 89 50 4E 47 are the magic number of PNG; 50 4E 47 spells "PNG" in ASCII. Magic numbers are how tools recognise a file regardless of its extension (file signatures). Writing a hex dump function yourself is the best way to learn the format:

Python Build a hex dump

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how the offset grows by 16 (0x10) each row, so 0x20 is byte number 32.

Endianness: which byte comes first?

Numbers longer than one byte can be stored two ways. Little-endian puts the least significant byte first (used by x86 and ARM in normal operation, and by NTFS and FAT). Big-endian puts the most significant byte first (network protocols, many file formats such as PNG chunk lengths).

The 32-bit number 1,000 is 0x000003E8:

  • big-endian bytes: 00 00 03 E8
  • little-endian bytes: E8 03 00 00

If you read little-endian bytes as big-endian you get nonsense (0xE8030000 = 3,892,510,720). When you decode a field from a structure, always check the format's documented byte order.

flowchart LR
    V["Value 0x000003E8<br/>(1000)"] --> BE["Big-endian<br/>00 00 03 E8"]
    V --> LE["Little-endian<br/>E8 03 00 00"]

Python's struct module and int.from_bytes handle both orders:

Python Decode integers with both byte orders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last record decodes to size 1,000,000 (0x000F4240), a good reminder that a tiny pattern of bytes can carry a lot of meaning.

Text: ASCII, UTF-8, UTF-16

Bytes become text through an encoding. ASCII covers English letters in one byte each. UTF-8 extends ASCII and uses 1 to 4 bytes per character. UTF-16 uses 2 or 4 bytes; Windows stores file names and many strings this way, so searching a Windows image for an ASCII keyword can miss them: the word secret in UTF-16LE is 73 00 65 00 63 00 ..., with a zero byte between letters. Searching for both encodings is routine.

Python The same word in three encodings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Bitwise thinking: flags

Many fields are bit flags: each bit means something. For example, a file attribute byte might use bit 0 for read-only, bit 1 for hidden and bit 2 for system. You test a flag with a bitwise AND.

Python Decode attribute flags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many hexadecimal digits represent one byte?

    Each hex digit is 4 bits, so a byte (8 bits) is two digits, from 00 to FF.
  2. 2. The bytes E8 03 00 00 are a little-endian 32-bit integer. What is its value?

    Reversing to 00 00 03 E8 gives 0x3E8, which is 1000.
  3. 3. Why might a search for the ASCII word 'secret' miss it in a Windows image?

    Windows commonly uses UTF-16LE, so the byte pattern differs from ASCII.
  4. 4. What does a . in the right-hand column of a hex dump usually mean?

    Dumps replace bytes outside the printable range with dots.

Summary

  • Hex shows each byte as two digits; a hex dump adds offsets and an ASCII column.
  • Magic numbers at the start of a file identify its type.
  • Multi-byte numbers are little-endian or big-endian; always check which the format uses.
  • Text may be ASCII, UTF-8 or UTF-16; search for the encodings that matter.
  • Flags pack several yes/no values into one number; test them with bitwise AND.

Next: Types of digital evidence and the order of volatility