dotFusion.net

A Simple Morse Code Converter

Posted on: January 18th, 2009.
Category: C#

Hello everybody, yesterday I wrote for you a simple Morse code to Text & Text to Morse code converter.
 

It’s 100% Morse code compliant and it supports only numbers, letters and space.
As you may already know the official Morse code specification does not provide symbols for any special characters (?.!=+- for example). But if you are interested – write me a line and I may add some of them to the next version of the example program.

 

Source code:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace MorseCodeConverter
{
    class MorseCodeCore
    {
        private Char[] Letters = new Char[] {'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', ' '};
        private String[] MorseCode = new String[] {".-", "-...", "-.-.",
          "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", 
          "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", 
          "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", 
          "...--", "....-", ".....", "-....", "--...", "---..", "----.", " "};
 
        public String ConvertTextToMorse(String text)
        {
            text = text.ToLower();
            String result = "";
            int index = -1;
            for (int i = 0; i <= text.Length - 1; i++)
            {
                index = Array.IndexOf(Letters, text[i]);
                if (index != -1)
                    result += MorseCode[index] + " ";
            }
 
            return result;
        }
 
        public String ConvertMorseToText(String text)
        {
            text = "@" + text.Replace(" ", "@@") + "@";
            int index = -1;
            foreach (Char c in Letters)
            {
                index = Array.IndexOf(Letters, c);
                text = text.Replace("@" + MorseCode[index] + "@", "@" + c.ToString() + "@");
            }
 
            return text.Replace("@@@@", " ").Replace("@", "");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            MorseCodeCore Morse = new MorseCodeCore();
 
            Console.WriteLine("Converting from text to morse: ");
            String str = Morse.ConvertTextToMorse("string to convert to morse code") ;
            Console.WriteLine(str);
 
            Console.WriteLine("\nConverting from morse to text: ");
            str = Morse.ConvertMorseToText("... - .-. .. -. --.   - ---   -.-." + 
              " --- -. ...- . .-. -   -... .- -.-. -.-   - ---   - . -..- -");
            Console.WriteLine(str);
 
            Console.ReadKey();
        }
    }
}

 
Download the example Source Code (MS Visual C# Express Edition Project)

This is a really simple example. If you think of a more sophisticated one, you may send it to me, and I will publish it with your name and email/website…

6 Responses to “A Simple Morse Code Converter”:

  1. parce necesito el codigo para c++…….
    es posible q un compilador lo tradusca o q debo hacer???
    ademas necesito poner sonido a la codificacion…..
    si me ayuda seria demasiado valioso gracias…….

  2. Hi,
    thanks for this great idea, however, I tried to load your example but it never functioned. There are no compiler errors but when I run it the screen shows all the \WriteLine\ Texts in the console and then closes.

    How is it supposed to react when I execute it, is it supposed to convert the characters I type in before pressing the return key?

    Could you also please comment some of the program lines to give a brief explanation?

    Thanks for your care,
    Best Regards,
    Mike

  3. Hi there Mike. Actually you can try to run it using the Ctrl+F5 key combination. Then it should pause after the execution is completed and you should be able to see the results.

    Keep in mind that this is just an example of the CORE functions needed for building a Morse code converter. Building up an interface is up to you.

     
    Have fun :)

  4. I was thinking about using this for a Text>Encryption>Your program>converter of …— to audio>HamRadio> PC with ham receiver>Audio to …—>Your Program>decrypt> Text

    I was thinking about adding all keyboard possibilities( !@#$%^&*():”;’,./?1234567890-={}|[]\~`). with their own unique pattern along with a unique pitch for a decider between characters.

    So “a” would be .- < with each having their own unique sound length.

    do you know of any programs that can take symbols and run a specific audio File based on those symbols provided?

    or just what code would be needed to embed Audio for symbols?

  5. Hi,
    This is n realy cool application, I just want to know, what is the use for the ‘@’ in the morse to text method what does it do and why do you use it?
    Thanks!

  6. It’s used as a temporary replacement :)

Leave a Reply