B4J Tutorial WebApps - Let's create a Morse Code Translator

Hi there

Let's explore how we can create a Morse code translator using Maps.

1. For this I have created a simple keyboard...

MorseCodeTranlator.gif


Related Content: http://www.learnmorsecode.com/

Text2Morse Translator

erel.gif
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Step 2: we need to map each key being pressed to its equivalent morse code. For that we will use a map.

Here we build a dictionary.

B4X:
Sub MorseKeys As Map
    Dim table As Map = CreateMap()
    table.Put("A", ".-")
    table.Put("B", "-...")
    table.Put("C", "-.-.")
    table.Put("D", "-..")
    table.Put("E", ".")
    table.Put("F", "..-.")
    table.Put("G", "--.")
    table.Put("H", "....")
    table.Put("I", "..")
    table.Put("J", ".---")
    table.Put("K", "-.-")
    table.Put("L", ".-..")
    table.Put("M", "--")
    table.Put("N", "-.")
    table.Put("O", "---")
    table.Put("P", ".--.")
    table.Put("Q", "--.-")
    table.Put("R", ".-.")
    table.Put("S", "...")
    table.Put("T", "-")
    table.Put("U", "..-")
    table.Put("V", "...-")
    table.Put("W", ".--")
    table.Put("X", "-..-")
    table.Put("Y", "-.--")
    table.Put("Z", "--..")
    table.Put("Á", ".--.-")            'A with acute accent
    table.Put("Ä", ".-.-")            'A with diaeresis
    table.Put("É", "..-..")            'E with acute accent
    table.Put("Ñ", "--.--")            'N with tilde
    table.Put("Ö", "---.")            'O with diaeresis
    table.Put("Ü", "..--")            'U with diaeresis
    table.Put("1", ".----")            
    table.Put("2", "..---")
    table.Put("3", "...--")
    table.Put("4", "....-")
    table.Put("5", ".....")
    table.Put("6", "-....")
    table.Put("7", "--...")
    table.Put("8", "---..")
    table.Put("9", "----.")
    table.Put("0", "-----")
    table.Put(",", "--..--")        'comma
    table.Put(".", ".-.-.-")        'period
    table.Put("?", "..--..")        'question mark
    table.Put(";", "-.-.-")            'semicolon
    table.Put(":", "---...")        'colon
    table.Put("/", "-..-.")            'slash
    table.Put("-", "-....-")        'dash
    table.Put("'", ".----.")        'apostrophe
    table.Put("()", "-.--.-")        'parenthesis
    table.Put("_", "..--.-")        'underline
    table.Put("@", ".--.-.")        'at symbol from http://www.learnmorsecode.com/
    table.Put(" ", "   ")    '3 spaces
    Return table
End Sub

The next thing we need to do is to interpret what is on the keyboard and then translate it to Morse code. One can then do a reverse translator that can receive a morse code and translate it into text.
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Translating Text to Morse Code

We take what is typed and then convert it to morse code.

B4X:
Sub TranslateToMorse(content As String) As String
    Dim mkeys As Map = MorseKeys
    'we will assign everything to a stringbuilder
    Dim res As StringBuilder
    res.Initialize 
    'make content uppercase
    content = content.ToUpperCase
    'length of the result
    Dim slen As Int = content.Length
    Dim i As Int = 0
    For i = 0 To slen - 1
        'get the content at position
        Dim code_in As String = content.CharAt(i)
        If mkeys.ContainsKey(code_in) Then
             Dim code_out As String = mkeys.Get(code_in)
            res.Append(code_out)
        End If
    Next
    Return res.ToString
End Sub

Happy B4x Coding!
 
Last edited:
Top