Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. <# gkostoulias(at)gmail.com
  2. 'ps.morsecode.ps1' Morse Code Encoder/Decoder #>
  3.  
  4. <#
  5. Input Morse code or plain text using "." for a dit and "-" or "_" for a dah.
  6. Letters in Morse code should be separated by spaces and words by "/" or "|".
  7. #>
  8.  
  9.  
  10. function MorseCode
  11. {
  12. [cmdletbinding()]
  13. param (
  14. [parameter(mandatory=$true,
  15. valuefrompipeline=$true)]
  16. [string]$input
  17. )
  18.  
  19. $indexArray =@(
  20. 'a','b','c','d','e','f','g','h','i','j','k',
  21. 'l','m','n','o','p','q','r','s','t','u','v',
  22. 'w','x','y','z','0','1','2','3','4','5','6',
  23. '7','8','9'
  24. )
  25. $morseArray =@(
  26. '.-','-...','-.-.','-..','.','..-.','--.',
  27. '....','..','.---','-.-','.-..','--','-.',
  28. '---','.--.','--.-','.-.','...','-','..-',
  29. '...-','.--','-..-','-.--','--..','-----',
  30. '.----','..---','...--','....-','.....',
  31. '-....','--...','---..','----.'
  32. )
  33.  
  34. if ($input -match "[a-z]")
  35. {
  36. ($input.ToCharArray() | %{ ($_ + ':') }).ToCharArray() |
  37. % {
  38. if ($_ -eq ':')
  39. {
  40. #letter timing
  41. write-host ' ' -n
  42. sleep -m 300
  43. return
  44. }
  45. if ($_ -eq ' ')
  46. {
  47. #word timing = (letter + word timing)
  48. write-host '|' -n
  49. sleep -m 600
  50. return
  51. }
  52.  
  53. $i = $indexArray.indexof("$_")
  54. $morseArray[$i] -split "(.)" |
  55. % {
  56. if ($_ -eq '.') { dit }
  57. if ($_ -eq '-') { dah }
  58. }
  59. }
  60. write ''
  61. }
  62. else {
  63. $input | %{ $_.split(" ")} | %{ $_ -replace "_","-" } |
  64. % {
  65. if ($_ -eq '/' -OR $_ -eq '|')
  66. {
  67. $charArray += ' '
  68. return
  69. }
  70. if (!($morseArray.contains("$_")))
  71. {
  72. $charArray += '?'
  73. return
  74. }
  75. $i = $morseArray.indexof("$_")
  76. $charArray += $indexArray[$i]
  77. }
  78. write-host "$charArray"
  79. }
  80. }
  81.  
  82. function dit
  83. {
  84. write-host "." -n
  85. [console]::beep(600,100)
  86. }
  87.  
  88. function dah
  89. {
  90. write-host "-" -n
  91. [console]::beep(600,300)
  92. }
  93.  
  94.  
  95. $args[0] | MorseCode
  96.  
  97. remove-variable * -erroraction silentlycontinue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement