All works

2022Java utility

Morse

A tiny Java class that encodes text to Morse code and decodes it back. One of my very first public repositories.

  • Java

Overview

Morse.toMorse(text) encodes letters, digits and a little punctuation; Morse.reverse(code) decodes them back, with a slash between words.

It's a small project, but it's where I started sharing code on GitHub, back in 2022.

See it in action

  1. A jshell session encoding Truong Son to Morse code and decoding it back to truong son
    Morse.java loaded into jshell: my name to Morse code and back again.

Highlights

  • 40 symbols

    Letters, digits, spaces and common punctuation.

  • 2022 where it started

    One of the first repositories on my GitHub.

From the source

Morse.java
public static String toMorse(String str) {
    StringBuilder result = new StringBuilder();
    str = str.toLowerCase(Locale.ROOT);

    char[] characters = str.toCharArray();
    for (int i = 0; i < characters.length; i++) {
        String c = String.valueOf(characters[i]);

        if (alphabet.containsKey(c))
            result.append(alphabet.get(c));

        if (i != characters.length - 1)
            result.append(" ");
    }

    return String.valueOf(result);
}

Ready to build something amazing?

I'd love to connect with you!