RazerX

Morse Code Utility

Jan 15th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.95 KB | None | 0 0
  1. Option Explicit On
  2.  
  3. Imports System.Text
  4.  
  5. ''' <summary>
  6. ''' BeepBeepMorse is a simple class that will help you convert strings to morse and vice versa.
  7. ''' It supports Alphanumericals and some special chars.
  8. ''' Coded By SubGenius @Hackforums.net
  9. '''
  10. ''' 16/1/14
  11. ''' Now you can play the morse codes.
  12. ''' Currently using the console.beep sub.
  13. '''
  14. '''  *** SOS ***
  15. ''' To add new characters to the "library" you must add the normal character and the corresponding new string in the same location.
  16. ''' For example. the period character in "Alpahabet" has the coresponding morse character in the same position in "Morse".
  17. ''' or the "Space"(" ") character corresponds to the backslash, many online Morse compilers use backslash to identify the space char.
  18. ''' *** SOS ***
  19. ''' </summary>
  20. ''' <remarks></remarks>
  21. Public Class MorseClass
  22.  
  23.     Dim Alphabet As String() = {" ", ".", ",", ":", "?", "'", "/", "@", "()", "=", "-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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"}
  24.     Dim Morse As String() = {"/", ".-.-.-", "--..--", "---...", "..--..", ".----.", "-..-.", ".--.-.", "-.--.-", "-...-", "-....-", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
  25.  
  26.     Private _PlayToneLength As Integer = 100
  27.     Property PlayToneLength As Integer
  28.         Set(value As Integer)
  29.             _PlayToneLength = value
  30.         End Set
  31.         Get
  32.             Return _PlayToneLength
  33.         End Get
  34.     End Property
  35.  
  36.     Private _Seperator As Char
  37.     Property Seperator As Char
  38.         Set(value As Char)
  39.             _Seperator = value
  40.         End Set
  41.         Get
  42.             Return _Seperator
  43.         End Get
  44.     End Property
  45.  
  46.  
  47.     ''' <summary>
  48.     ''' Creates a new Morse Class
  49.     ''' </summary>
  50.     ''' <param name="SeperatorChar">This char will seperate the different morse strings.</param>
  51.     ''' <remarks></remarks>
  52.     Sub New(Optional ByVal SeperatorChar As Char = " ")
  53.         _Seperator = SeperatorChar
  54.     End Sub
  55.     ''' <summary>
  56.     ''' Converts an normal alphabet character to morse character.
  57.     ''' </summary>
  58.     ''' <param name="character">The character you want to convert to morse.</param>
  59.     ''' <returns></returns>
  60.     ''' <remarks></remarks>
  61.     Function GetMorseChar(ByVal character As Char) As String
  62.         For i As Integer = 0 To (Alphabet.Length - 1)
  63.             If Alphabet(i) = character Then
  64.                 Return Morse(i) : Exit For
  65.             End If
  66.         Next
  67.         Return Nothing
  68.     End Function
  69.  
  70.     ''' <summary>
  71.     ''' Converts an morse character to normal alphabet character.
  72.     ''' </summary>
  73.     ''' <param name="text">The morse character you want to convert to normal alphabet char.</param>
  74.     ''' <returns></returns>
  75.     ''' <remarks></remarks>
  76.     Function GetAlphabetChar(ByVal text As String) As Char
  77.         For i As Integer = 0 To (Morse.Length - 1)
  78.             If Morse(i) = text Then
  79.                 Return CChar(Alphabet(i)) : Exit For
  80.             End If
  81.         Next
  82.         Return Nothing
  83.     End Function
  84.  
  85.     ''' <summary>
  86.     ''' Converts the selected text to morse string.
  87.     ''' </summary>
  88.     ''' <param name="NormalText">The morse string you want to convert.</param>
  89.     ''' <returns></returns>
  90.     ''' <remarks></remarks>
  91.     Function CompileText(ByVal NormalText As String) As String
  92.         Dim Builder As New StringBuilder
  93.         For Each character As Char In NormalText
  94.             Builder.Append(GetMorseChar(character) & Seperator)
  95.         Next
  96.         Return Builder.ToString
  97.     End Function
  98.  
  99.     ''' <summary>
  100.     ''' Converts the selected morse text to normal string.
  101.     ''' </summary>
  102.     ''' <param name="MorseText">The morse string you want to convert.</param>
  103.     ''' <returns></returns>
  104.     ''' <remarks></remarks>
  105.     Function CompileMorse(ByVal MorseText) As String
  106.         Dim CharsSplitted As String() = MorseText.Split(Seperator)
  107.         Dim Builder As New StringBuilder
  108.         For Each character As String In CharsSplitted
  109.             Builder.Append(GetAlphabetChar(character))
  110.         Next
  111.         Return Builder.ToString
  112.     End Function
  113.  
  114.     Sub PlayMorse(ByVal MorseString As String)
  115.  
  116.         For Each character As Char In MorseString
  117.             Select Case character
  118.                 Case CChar(".")
  119.                     Console.Beep(700, _PlayToneLength)
  120.                 Case CChar("-")
  121.                     Console.Beep(700, 4 * _PlayToneLength)
  122.                 Case Seperator
  123.                     Threading.Thread.Sleep(5 * _PlayToneLength)
  124.                 Case Else
  125.                     Threading.Thread.Sleep(3 * _PlayToneLength)
  126.             End Select
  127.         Next
  128.  
  129.     End Sub
  130.  
  131. End Class
Advertisement
Add Comment
Please, Sign In to add comment