Samuel Morse solved the same problem that Huffman solved with his coding algorithm in 1952: assign shorter codes to more frequent characters. The letter E (the most common in English) is a single dot. The letter J is dot-dash-dash-dash.
The encoding table
const MORSE = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..',
'E': '.', 'F': '..-.', 'G': '--.', 'H': '....',
'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.',
'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.'
};
The variable-length insight
Morse assigned the shortest codes to the most frequent English letters. E (12.7% frequency) is a single dot. T (9.1%) is a single dash. Compare that to Q (0.1%) which is dash-dash-dot-dash.
This is the same principle behind Huffman coding, invented over a century later. Frequently occurring symbols get shorter codes, minimizing total message length. Morse figured this out empirically by counting letter frequencies in a printer's type case.
Encoding and decoding
function textToMorse(text) {
return text.toUpperCase().split('').map(char => {
if (char === ' ') return '/';
return MORSE[char] || '';
}).join(' ');
}
function morseToText(morse) {
const REVERSE = Object.fromEntries(
Object.entries(MORSE).map(([k, v]) => [v, k])
);
return morse.split(' / ').map(word =>
word.split(' ').map(code => REVERSE[code] || '').join('')
).join(' ');
}
The convention is: spaces between letters, slashes (or triple spaces) between words.
Timing in audio Morse
When generating audible Morse code, the timing ratios matter:
- Dot duration: 1 unit
- Dash duration: 3 units
- Space between elements of same letter: 1 unit
- Space between letters: 3 units
- Space between words: 7 units
At 20 words per minute (standard proficiency), one unit is 60 milliseconds.
For translating text to and from Morse code with audio playback, I built a translator at zovo.one/free-tools/morse-code-translator. It handles both directions and plays the encoded message as audio with proper timing ratios.
I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.
Top comments (0)