Morse Code: The Encoding Math Behind the Dots and Dashes
Morse code is the oldest character encoding scheme developers still touch today. It is also, quietly, one of the most elegant: a variable-length, self-optimising prefix code built on a two-symbol alphabet more than 150 years before Huffman coding became a textbook chapter. If you have ever compressed data, tuned a regex, or shaved bytes off a payload, you have been solving the same problem Samuel Morse and Alfred Vail solved in 1838.
The problem Vail actually solved
The popular story says Morse counted letters in a printer's font drawer and assigned the shortest codes to the most common ones. The truth is messier: Vail optimised against letter frequency and, probably, against the number of mechanical strokes a telegraph key required. Either way, the result looks like an early frequency-sorted dictionary:
- E — the most common English letter — got a single dot:
. - T — the second most common — got a single dash:
- - Z — rare — drew the shortest straw:
--.., four symbols long
Average code length lands around 3 units per letter for running English text. Naive fixed-length encoding of 26 letters needs at least 5 units per letter (2^4 = 16, not enough; 2^5 = 32). Morse's variable length buys roughly a 40% reduction on the wire — with zero compression libraries and zero computing power.
One catch makes this work: it is a prefix code. No code is a prefix of another (. = E, and no other code starts with a lone dot followed by letter-gap ambiguity — the gaps carry that weight). Decode this way: read symbols until the rules say a letter is complete. If E lasted four units and another code began with one dot, the decoder would not know where the boundary sits. Variable length without the prefix property is noise, and this is exactly the distinction between Morse's design and naive variable-length schemes that die on ambiguity.
The timing rules are the real spec
The dots and dashes get all the attention; the timing is what makes code decodable. All durations are defined relative to one "dit", the dot:
- Dot: 1 unit
- Dash: 3 units
- Gap between symbols inside a letter: 1 unit
- Gap between letters: 3 units
- Gap between words: 7 units
These ratios are not decoration. They set the signal-to-pace trade-off operators call weighting. Send code with strict 1:3:7 ratios ("Farnsworth spacing" stretches the gaps, not the elements, to make it learnable at speed) and your effective words-per-minute diverges from your character-per-minute rate. Radio training programs exploit this deliberately: students learn to hear characters at 20 WPM with gaps that make the overall flow 5–10 WPM, because the skill that transfers is pattern recognition per character, not counting dots.
This is also where implementations break in practice. Common real-world mistakes:
- Merging the 1-unit and 3-unit gaps. "SOS" sent as a single unbroken stream of
...---...is technically invalid Morse and decoders will read it as one (often garbage) letter cluster. The letter gaps are semantic, not cosmetic. - Ignoring ambiguity in hand-typed input. Users paste
". ."or".-."with no letter separators at all, and a decoder that assumes letters is lying to them. A robust tool either demands a separator convention or reports where alternatives exist. - Handling digits and punctuation as an afterthought. Digits are all exactly five units long (fixed length — a deliberate exception, because numbers in traffic like callsigns and coordinates cannot tolerate misreads), and prosigns like
AR(end of message) andSK(end of contact) reuse letter sequences with no spacing between them.
When you encode or decode text for training material, captions, or accessibility features, run the round trip. Encode a message, feed it back through the decoder, and compare — the round trip is how you catch a spacing implementation bug that looks fine to the eye. A dedicated morse code converter handles the gaps, digits, and prosigns correctly out of the box, which is the fastest way to generate trustworthy practice sheets or decode an old message.
Why binary comparisons make this click
Morse is binary alphabetically — two symbols — but not binary positionally. A bit string fixes symbol positions; Morse fixes nothing and relies on timing gaps instead. That difference is worth internalising because it is the reason binary encodings dominate machine storage and Morse dominated human-speed radio.
Consider the letter A:
- Morse:
.-— one dot, one dash, then a 3-unit silence. The information is in when the symbols occur. - ASCII:
01000001— 8 fixed bits. The information is in which positions are on.
Sent over a noisy channel at human hearing speed, Morse wins: variable-length common symbols are shorter, timing gaps reset the decoder, and a trained operator can read through interference that kills clocked binary transmission. Sent inside a machine, binary wins: fixed length means no timing analysis, trivially cheap hardware, and error-detecting framing. Same alphabet of two, opposite engineering bets. If you want to see the fixed-length side of the trade-off hands on, encode the same letter with a binary converter and count the bits; then check the same character in hex or octal with a number base converter — the contrast between variable-length timing codes and fixed-length positional codes becomes immediately visible.
The history rhymes too. Huffman's algorithm, proven optimal in 1952, produces the same class of object Morse built by hand: a prefix code where probable symbols get short codes. Morse is not optimal by Huffman's standard — I (.., 2 units) outranks A (.-, 2 units) in frequency but pays the same — but it is within striking distance for a design made without computers.
Where morse still earns its keep
- Radio and HF communication. CW (continuous wave) modes still beat voice at low signal-to-noise ratios: a 5 W signal carrying Morse can be decoded where SSB voice is unintelligible.
- Accessibility hardware. Google's Morse-based Gboard input mode lets people with limited motor control type at parity with QWERTY scanning. Two switches, two symbols, full alphabet.
- Assistive signalling and education. Escape-room props, firmware status blinkies (a device that blinks an error in Morse on its LED when it has no display), and teaching material all reuse it because the alphabet is small and printable.
- Legacy encoding checks. Old archives, naval signals, and museum-grade datasets still carry Morse payloads; anyone migrating them hits the same gap-handling questions described above.
Practical checklist before you ship code that touches Morse
- Pin your timing to the 1:3:7 ratios in writing, in config, and in tests. "Close enough" spacing is how decoders end up guessing.
- Define your separator convention for letter and word boundaries explicitly — space, slash, or explicit gap counts — and validate it in input.
- Test the round trip: every supported character must survive encode → decode → encode unchanged.
- Handle digits and prosigns in the same pass, not as a follow-up patch. They are where the fixed-length exceptions live.
- If you ever need a different encoding of the same two-symbol idea — binary, hex, base32 — reach for an existing tool instead of hand-rolling tables. Reused, audited converters are how spacing bugs stay out of production.
Morse survives not because it is nostalgic but because it is a genuinely good piece of encoder design: variable length, prefix-safe, optimised for its channel, and simple enough to learn in an afternoon. Treat its timing rules with the same respect you give a binary protocol spec, and both the history and the math pay off.