Lab: Inspect Meaning In Bytes
Goal
Use local commands to see that files contain bytes, and that the interpretation of those bytes depends on a code or convention.
Setup
These commands are macOS-friendly and use common local tools:
python3xxdprintffile
Work in /tmp so the lab does not modify the repository.
mkdir -p /tmp/from-bits-to-meaning-lab
cd /tmp/from-bits-to-meaning-lab
Step 1: Make A Tiny Text File
printf 'Hi, 12!\n' > message.txt
xxd -g 1 message.txt
file message.txt
Observe:
xxdshows bytes in hexadecimal.- In ASCII-compatible text,
His one byte,iis one byte, punctuation is one byte, and the digit characters1and2are bytes too. - The file does not store the abstract number twelve. It stores character codes for the text symbols
1and2.
Step 2: Ask Python To Show Several Interpretations
python3 - <<'PY'
data = b"Hi, 12!\n"
for index, byte in enumerate(data):
char = chr(byte) if 32 <= byte <= 126 else repr(chr(byte))
print(index, hex(byte), format(byte, "08b"), byte, char)
PY
Observe:
- The same byte can be shown as hex, binary, decimal, or a character.
- These views are interpretations of the same stored pattern.
Step 3: Compare Text 12 With Binary Number 12
python3 - <<'PY'
from pathlib import Path
Path("number-as-text.txt").write_text("12\n", encoding="ascii")
Path("number-as-one-byte.bin").write_bytes(bytes([12]))
Path("number-as-two-byte-little.bin").write_bytes((12).to_bytes(2, "little"))
PY
xxd -g 1 number-as-text.txt
xxd -g 1 number-as-one-byte.bin
xxd -g 1 number-as-two-byte-little.bin
file number-as-text.txt number-as-one-byte.bin number-as-two-byte-little.bin
Observe:
number-as-text.txtstores character codes for the symbols1,2, and newline.number-as-one-byte.binstores the numeric value 12 in one byte.number-as-two-byte-little.binstores the numeric value 12 in two bytes using little-endian byte order.
This connects to Petzold’s distinction between ASCII text files and binary files.
Step 4: Same Byte, Different Stories
python3 - <<'PY'
byte = 0x41
print("hex:", hex(byte))
print("binary:", format(byte, "08b"))
print("unsigned integer:", byte)
print("ASCII-compatible character:", chr(byte))
print("as two 4-bit nibbles:", format(byte >> 4, "04b"), format(byte & 0x0F, "04b"))
PY
Observe:
0x41can be read as 65 or asA.- Nothing inside the byte forces one interpretation.
- The interpreter supplies the meaning.
Step 5: Pack Eight Black/White Pixels Into One Byte
python3 - <<'PY'
pixels = [0, 1, 0, 1, 1, 0, 0, 1]
value = 0
for bit in pixels:
value = (value << 1) | bit
print("pixels:", pixels)
print("packed byte:", hex(value), format(value, "08b"), value)
print("visual:", "".join("#" if bit else "." for bit in pixels))
PY
Observe:
- The same eight bits can be treated as a row of black/white pixels.
- A display or image format contract decides which bit corresponds to which pixel and which value means black or white.
Reflection
Answer these in your own words:
- Where did interpretation enter in each step?
- Which bytes were easy for you to read as text, and why?
- Why did the binary representation of number 12 not look like the text representation of
12? - How is a pixel row similar to a byte of text, and how is it different?
- What would a program need to know before deciding whether bytes are text, numbers, image data, or instructions?
Source Anchors
petzold-code-hidden-language-computer-hardware-software-2eChapter 15. Bytes and Hex, near page 170 through page 180: byte as 8 bits and one of 256 possible things.Chapter 20. ASCII and a Cast of Characters, near page 271 through page 285: ASCII character codes and text strings.Chapter 22. The Operating System, near page 310 through page 311: binary files versus text files and numeric values represented as ASCII text.Chapter 25. The Graphical Revolution, near page 344 through page 353: pixels, bitmaps, bits per pixel, and binary representation of visual information.
nisan-schocken-elements-of-computing-systems-2eChapter 2 Boolean Arithmetic, near page 60 through page 62: binary numbers, human decimal conversion, word size.Chapter 3 Memory, near page 85 through page 88: 1-bit register, 16-bit register, and RAM.Chapter 4 Machine Language, near page 114 through page 115: Hack screen and keyboard memory maps.Chapter 12 Operating System, near page 340 through page 347: screen memory map, pixel bits, character bitmaps.
Open Questions
- This lab uses ASCII-compatible examples. For modern Unicode and UTF-8 byte inspection, use the sibling topic
../unicode-and-utf-8/. - The
filecommand uses heuristics; it is useful for inspection, but it is not the source of truth for what bytes “really mean.”