Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# gkostoulias(at)gmail.com
- 'ps.morsecode.ps1' Morse Code Encoder/Decoder #>
- <#
- Input Morse code or plain text using "." for a dit and "-" or "_" for a dah.
- Letters in Morse code should be separated by spaces and words by "/" or "|".
- #>
- function MorseCode
- {
- [cmdletbinding()]
- param (
- [parameter(mandatory=$true,
- valuefrompipeline=$true)]
- [string]$input
- )
- $indexArray =@(
- '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'
- )
- $morseArray =@(
- '.-','-...','-.-.','-..','.','..-.','--.',
- '....','..','.---','-.-','.-..','--','-.',
- '---','.--.','--.-','.-.','...','-','..-',
- '...-','.--','-..-','-.--','--..','-----',
- '.----','..---','...--','....-','.....',
- '-....','--...','---..','----.'
- )
- if ($input -match "[a-z]")
- {
- ($input.ToCharArray() | %{ ($_ + ':') }).ToCharArray() |
- % {
- if ($_ -eq ':')
- {
- #letter timing
- write-host ' ' -n
- sleep -m 300
- return
- }
- if ($_ -eq ' ')
- {
- #word timing = (letter + word timing)
- write-host '|' -n
- sleep -m 600
- return
- }
- $i = $indexArray.indexof("$_")
- $morseArray[$i] -split "(.)" |
- % {
- if ($_ -eq '.') { dit }
- if ($_ -eq '-') { dah }
- }
- }
- write ''
- }
- else {
- $input | %{ $_.split(" ")} | %{ $_ -replace "_","-" } |
- % {
- if ($_ -eq '/' -OR $_ -eq '|')
- {
- $charArray += ' '
- return
- }
- if (!($morseArray.contains("$_")))
- {
- $charArray += '?'
- return
- }
- $i = $morseArray.indexof("$_")
- $charArray += $indexArray[$i]
- }
- write-host "$charArray"
- }
- }
- function dit
- {
- write-host "." -n
- [console]::beep(600,100)
- }
- function dah
- {
- write-host "-" -n
- [console]::beep(600,300)
- }
- $args[0] | MorseCode
- remove-variable * -erroraction silentlycontinue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement