Code archives/Miscellaneous/Morse Code Converter
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This program takes the text that you type in and writes onto the screen in Morse code. It also used the systembeep DLL to create the audible dots and dashs of Morse code. EDIT: Thanks to everybody who commented. Some of those were put into this edit. Keep them coming. | |||||
Graphics 1200,400,0,2 SetBuffer BackBuffer() Global PriMsg$ Global SndMsg$ Global command$ Global TKey% Global MorsMsg$ Const dot = 50 Const dash = 150 Const freq = 500 Dim textline$(100) Print "" While Not KeyDown(1) Cls Check_Keys() Text 0,0,">:" + PriMsg$ Text 0,24,MorsMsg Flip Wend Function Check_Keys() ;Grab any keys that get pressed TKey% = GetKey() If TKey <> 0 Then If Tkey <> 8 Then ;8 = backspace key ;convert pressed key To the actual character TChr$ = Chr$(TKey) MorsMsg = "" ;append String with Last key pressed PriMsg = PriMsg + TChr EndIf EndIf If KeyDown(14) If Len(PriMsg) > 0 Then PriMsg = Left(PriMsg,Len(PriMsg)-1) Delay(100) EndIf If KeyDown(200) Or KeyDown(208) PriMsg = PriMsg + "" EndIf ;hitting enter key will send contents of sndmsg to client/server If KeyHit(28) Then ;Enter key SndMsg = PriMsg MorseConvert() MorseSound() PriMsg = "" EndIf End Function Function MorseConvert() For i = 1 To Len(PriMsg) temp$ = Right(Left(PriMsg,i),1) If temp = "a" Or temp = "A" MorsMsg = MorsMsg + ".- " ElseIf temp = "b" Or temp = "B" MorsMsg = MorsMsg + "-... " ElseIf temp = "c" Or temp = "C" MorsMsg = MorsMsg + "-.-. " ElseIf temp = "d" Or temp = "D" MorsMsg = MorsMsg + "-.. " ElseIf temp = "e" Or temp = "E" MorsMsg = MorsMsg + ". " ElseIf temp = "f" Or temp = "F" MorsMsg = MorsMsg + "..-. " ElseIf temp = "g" Or temp = "G" MorsMsg = MorsMsg + "--. " ElseIf temp = "h" Or temp = "H" MorsMsg = MorsMsg + ".... " ElseIf temp = "i" Or temp = "I" MorsMsg = MorsMsg + ".. " ElseIf temp = "j" Or temp = "J" MorsMsg = MorsMsg + ".--- " ElseIf temp = "k" Or temp = "K" MorsMsg = MorsMsg + "-.- " ElseIf temp = "l" Or temp = "L" MorsMsg = MorsMsg + ".-.. " ElseIf temp = "m" Or temp = "M" MorsMsg = MorsMsg + "-- " ElseIf temp = "n" Or temp = "N" MorsMsg = MorsMsg + "-. " ElseIf temp = "o" Or temp = "O" MorsMsg = MorsMsg + "--- " ElseIf temp = "p" Or temp = "P" MorsMsg = MorsMsg + ".--. " ElseIf temp = "q" Or temp = "Q" MorsMsg = MorsMsg + "--.- " ElseIf temp = "r" Or temp = "R" MorsMsg = MorsMsg + ".-. " ElseIf temp = "s" Or temp = "S" MorsMsg = MorsMsg + "... " ElseIf temp = "t" Or temp = "T" MorsMsg = MorsMsg + "- " ElseIf temp = "u" Or temp = "U" MorsMsg = MorsMsg + "..- " ElseIf temp = "v" Or temp = "V" MorsMsg = MorsMsg + ".-- " ElseIf temp = "w" Or temp = "W" MorsMsg = MorsMsg + ".--.. " ElseIf temp = "x" Or temp = "X" MorsMsg = MorsMsg + "-..- " ElseIf temp = "y" Or temp = "Y" MorsMsg = MorsMsg + "-.-- " ElseIf temp = "z" Or temp = "Z" MorsMsg = MorsMsg + "--.. " ElseIf temp = " " MorsMsg = MorsMsg + "| " ElseIf temp = "1" MorsMsg = MorsMsg + ".---- " ElseIf temp = "2" MorsMsg = MorsMsg + "..--- " ElseIf temp = "3" MorsMsg = MorsMsg + "...-- " ElseIf temp = "4" MorsMsg = MorsMsg + "....- " ElseIf temp = "5" MorsMsg = MorsMsg + "..... " ElseIf temp = "6" MorsMsg = MorsMsg + "-.... " ElseIf temp = "7" MorsMsg = MorsMsg + "--... " ElseIf temp = "8" MorsMsg = MorsMsg + "---.. " ElseIf temp = "9" MorsMsg = MorsMsg + "----. " ElseIf temp = "0" MorsMsg = MorsMsg + "----- " EndIf Next End Function Function MorseSound() For i = 0 To Len(MorsMsg) temp$ = Right(Left(MorsMsg,i),1) If temp = "-" SystemBeep(freq, dash) ElseIf temp = "." SystemBeep(freq, dot) ElseIf temp = " " Delay(50) EndIf Delay(50) next End Function |
Comments
| ||
Instead of having a giant If statement, you could just parse the string you just created. |
| ||
Nice little toy :) I agree with Gosse though. parsing the string to generate the sounds would save a ton of code. pseudo code: Function MorseSound(code$) For every character in code if character is "-" then SystemBeep(freq, dash) else SystemBeep(freq, dot) end if Delay(50) next end Function |
| ||
You can also save a bunch of checks by changing this line:temp$ = Right(Left(PreMsg,i),1) to say this instead: temp$ = Upper(Right(Left(PreMsg,i),1)) That way everything will always be in uppercase, and you don't have to perform two checks for each character to account for both upper and lowercase possibilities. So instead of this: if temp = "a" or temp = "A" you can do this: if temp = "A" |
| ||
Just a couple questions and possibly a idea or two to shorten, and optimize your routines a bit. First, why are you using: temp$ = Right(Left(PreMsg,i),1) Will not this do the same thing, and make more sense: temp$ = Mid(PreMsg,i,1) ??? Optimize: You can kill a few birds with the following idea: Use an array (read from data statements) of all your input text to morse equivalent. Create a string such as "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ." Use InStr in your Key input routine, and Morse Convert function. In this way you can except only valid text input.... Also you can eliminate the entire If..ElseIf.. construct into just a few lines of code in the for loop. If you need example code, or if I need to explain these ideas better, then please feel free to respond either here, or email. *I think I thunk a thought that thunk a thought to think. |
| ||
the "Lite" version :) |
| ||
:D Ha ha. Good one Bobysait!!! Compact and to the point! Nice work... :) |
| ||
But I like this better: Function MorseConvert$(msg$) Local temp$="",i%=0,v%=0 Local teststring$="ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789" For i = 1 To Len(msg) v=Instr(teststring,Upper(Mid(msg,i,1))) If v Then temp$=temp + Morse(v-1)+" " Next Return temp End Function :) (By the way... (dumb question time...) how do you put the code in a editable, nice looking window in the post??? |
| ||
[ code] your code [/ code] for the full size code or [ codebox] your code [/ codebox] for a fixed height code box, with a scrollbar ( without the spaces ) ps : "Instr" is slower than computing asc() as the Instr function search from start to end of the string, and compute the ascci code for the char, and compare them to find the first identical with the one you pass in parameter. It's very faster to use asc. |
| ||
Thanks for the [ code ] info. :D Instr is probably slower... I was just trying to show superStruct that there ARE many ways of doing the same thing. And isn't a bit of healthy competition fun? ha ha I'm sure someone out there will see this post and put us all to shame! ha ha Laters, Be Safe! :)) P.S. Lets see this in the BF Language! ha ha |
| ||
And isn't a bit of healthy competition fun? ha ha Of course :) But mine is better :P There is probably another "better" way to do it, that also involve a bit more code. 1> Store the chars ( " ", "A" to "Z" and "0" to "9" ) with a dim like : Dim CharsV(256) i=0 For n=asc("A") to asc("Z") CharsV(n) = i i=i+1 next 2> instead of If v=XXX statement, just replace the V with CharsV(v), that is a direct access to all chars. Then, we'll probably get very near to the otpimal solution :) |
Code Archives Forum