Content · Programming · Python

Python Understand Binary Files with Hexadecimal Dumps

Inspect unknown binary bytes using a hexadecimal representation. A hex dump turns each byte into two hexadecimal digits, exposing headers and offsets without attempting to decode arbitrary bytes as text.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This Python tutorial explains Understand Binary Files with Hexadecimal Dumps. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.

What This Feature Does


Inspect unknown binary bytes using a hexadecimal representation.

A hex dump turns each byte into two hexadecimal digits, exposing headers and offsets without attempting to decode arbitrary bytes as text. Hex is a readable representation of bytes, not compression or encryption.

When to Use It


This technique belongs to binary data and byte operations. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.

Step-by-Step Python Example


Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.

file_header = b"\x89PNG\r\n\x1a\n"
print(file_header.hex(" "))
print(len(file_header))
print(file_header[:4])

Understanding the Code


  • Find the call print(file_header.hex(" ")). Its parameters determine the specific operation demonstrated in the title.
  • Its purpose is to inspect unknown binary bytes using a hexadecimal representation. Compare this description against the result printed by the program.
  • The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.

Expected Output


89 50 4e 47 0d 0a 1a 0a
8
b'\x89PNG'

Each printed line corresponds to a separate observation in the program. Use the lines to distinguish returned data, changes to existing objects, and status checks.

Important Details and Edge Cases


File signatures give hints, not a full validity check; confirm the remainder of the file before trusting its contents.

Bytes are not the same as text. Decide whether a value represents encoded characters or a binary record, and document its length, byte order and input assumptions before exchanging it with another program.

Second Example: A Useful Variation


binary_data = bytes.fromhex('00 ff 7f 20')
print(binary_data.hex(' '))
print(list(binary_data))

Each two-digit hexadecimal group corresponds to one byte, making offsets easy to inspect.

Variation Output


00 ff 7f 20
[0, 255, 127, 32]

Practice: Try It Yourself


Compare the first eight bytes of two known sample files using bytes.hex().

Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.

Official Python Documentation


For the complete reference, see the Python standard library documentation.

Common Beginner Mistakes


  • Calling decode("utf-8") on arbitrary binary data and assuming it is a text file.
  • Mistaking a hex dump for the exact characters a file would display in a text editor.
  • Bytes are not the same as text. Decide whether a value represents encoded characters or a binary record, and document its length, byte order and input assumptions before exchanging it with another program.

Key Takeaways


  • Inspect unknown binary bytes using a hexadecimal representation.
  • A hex dump turns each byte into two hexadecimal digits, exposing headers and offsets without attempting to decode arbitrary bytes as text. Hex is a readable representation of bytes, not compression or encryption.
  • Avoid this common pitfall: Calling decode("utf-8") on arbitrary binary data and assuming it is a text file.
Share this resourceSend this page to someone who may find it useful.