Project 1: Universal Base Converter
- File: P01-universal-base-converter.md
- Main Programming Language: C or Python
- Alternative Programming Languages: Rust, Go, JavaScript
- Coolness Level: Level 2 (See REFERENCE.md)
- Business Potential: Level 1 (See REFERENCE.md)
- Difficulty: Level 1 (See REFERENCE.md)
- Knowledge Area: Parsing, Numeric Representation
- Software or Tool: CLI
- Main Book: “Computer Systems: A Programmer’s Perspective”
What you will build: A CLI tool that converts numbers between decimal, binary, and hex.
Why it teaches binary/hex: You will implement the exact conversion algorithms that make these systems equivalent.
Core challenges you will face:
- Valid digit parsing -> Positional Systems
- Division/remainder conversion -> Positional Systems
- Output formatting -> Bits/Bytes/Nibbles
Real World Outcome
You can run the tool on any value and get verified conversions.
$ baseconv --from dec --to hex 255
FF
$ baseconv --from hex --to bin 7B
01111011
The Core Question You Are Answering
“How does the same value survive a change of representation without changing its meaning?”
This forces you to distinguish the value from its notation.
Concepts You Must Understand First
- Positional notation
- What does each digit position mean?
- Book Reference: “Computer Systems: A Programmer’s Perspective” - Ch. 2
- Hex digit set
- How do digits A-F map to decimal values?
- Book Reference: “Code” by Charles Petzold - Ch. 7-8
- Remainder-based conversion
- Why do remainders form digits in reverse?
- Book Reference: “Grokking Algorithms” - Ch. 1
Questions to Guide Your Design
- Input format
- How will you parse prefixes like 0x or 0b?
- How will you validate digit sets for each base?
- Conversion engine
- Will you convert via decimal or support direct base-to-base?
- How will you avoid overflow for large inputs?
Thinking Exercise
Manual Conversion Walkthrough
Trace the conversion of 123 (decimal) to binary by hand. Then reverse the process by parsing the binary result back to decimal.
Questions to answer:
- Where do remainders appear in the final string?
- How does the place-value expansion recover the original number?
The Interview Questions They Will Ask
- “Explain how you convert decimal to binary without using built-in functions.”
- “Why does hex map cleanly to binary?”
- “How would you validate a user-provided number in an arbitrary base?”
- “What is the time complexity of repeated-division conversion?”
- “How do you handle numbers that exceed standard integer sizes?”
Hints in Layers
Hint 1: Starting Point Use a left-to-right parser for base-to-decimal and a remainder loop for decimal-to-base.
Hint 2: Next Level Store digits as integers, then map 0-15 to 0-9/A-F during formatting.
Hint 3: Technical Details Pseudocode outline:
parse(digits, base):
acc = 0
for each digit:
acc = acc * base + value(digit)
return acc
format(value, base):
remainders = []
while value > 0:
remainders.append(value % base)
value = value / base
return reverse(remainders)
Hint 4: Tools/Debugging
Compare your outputs against python3 -c "print(hex(n))" for random inputs.
Books That Will Help
| Topic | Book | Chapter |
|---|---|---|
| Data representation | “Computer Systems: A Programmer’s Perspective” | Ch. 2 |
| Number systems | “Code” by Charles Petzold | Ch. 7-8 |
Common Pitfalls and Debugging
Problem 1: “Hex digits above 9 are rejected”
- Why: You only accept 0-9 in the digit parser.
- Fix: Extend the digit map to A-F and a-f.
- Quick test: Convert FF to 255 and back.
Definition of Done
- Converts between dec, bin, hex for all 0..65535
- Rejects invalid digits for a base
- Matches outputs from a trusted tool for random tests
- Includes a small test vector file