DEV Community

Austprokhan
Austprokhan

Posted on

Morse Code for Developers: Encoding Text as Dots and Dahs

As developers, we encode things all day — UTF-8, base64, URL encoding, JSON. Morse code is one of the oldest encoding schemes still in active use, and it's a genuinely fun one to implement yourself.

How the encoding works

Each character maps to a sequence of dits (.) and dahs (-):

  • A = .-, B = -..., S = ..., O = ---
  • The most famous sequence, SOS, is ...---...

The part most tutorials skip is the timing rules — and this is what makes machine-readable Morse actually work:

  • Dit = 1 time unit, dah = 3 units
  • Gap between symbols within a letter = 1 unit
  • Gap between letters = 3 units
  • Gap between words = 7 units

Get the timing wrong and your decoder (human or machine) can't tell where one letter ends and the next begins. It's a 19th-century lesson in framing and delimiters that still applies to protocol design today.

Where developers run into it

  • CTFs and puzzle hunts: Morse shows up constantly, sometimes disguised as audio, blinking lights, or even long/short pauses in text.
  • Embedded and IoT: blinking an LED in Morse is the "hello world" of hardware signaling.
  • Accessibility tech: single-switch Morse input lets people with very limited mobility type full sentences.
  • Amateur radio: CW (Morse) remains the most robust weak-signal mode — it gets through when voice can't.

Try it in code

Here's a minimal text-to-Morse encoder in JavaScript:

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: "--..",
  1: ".----", 2: "..---", 3: "...--", 4: "....-",
  5: ".....", 6: "-....", 7: "--...", 8: "---..",
  9: "----.", 0: "-----"
};

const toMorse = (text) =>
  text.toUpperCase().split("")
    .map(ch => ch === " " ? "/" : (MORSE[ch] || "?"))
    .join(" ");

console.log(toMorse("SOS")); // ... --- ...
Enter fullscreen mode Exit fullscreen mode

Once you've built it, check your output against a morse code translator to verify your patterns match the standard encoding.

Why bother

It's a 180-year-old protocol that still teaches clean lessons about encoding, timing, and graceful degradation under noise. Worth an evening of tinkering — and the next time Morse shows up in a CTF, you'll read it cold.

Top comments (0)