Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created March 15, 2025 17:46
Show Gist options
  • Save dfinke/ec5a32a2898af67b42f71b4f82f8ca72 to your computer and use it in GitHub Desktop.
Save dfinke/ec5a32a2898af67b42f71b4f82f8ca72 to your computer and use it in GitHub Desktop.
A PowerShell script to convert text to Morse code and vice versa
function Convert-TextToMorse {
param (
[Parameter(Mandatory = $true)]
[string]$Text
)
$morseDict = @{
'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' = '--..'; '1' = '.----'; '2' = '..---'; '3' = '...--'; '4' = '....-'
'5' = '.....'; '6' = '-....'; '7' = '--...'; '8' = '---..'; '9' = '----.'; '0' = '-----'
' ' = '/'; '.' = '.-.-.-'; ',' = '--..--'; '?' = '..--..'; "'" = '.----.'; '!' = '-.-.--'
'/' = '-..-.'; '(' = '-.--.'; ')' = '-.--.-'; '&' = '.-...'; ':' = '---...'; ';' = '-.-.-.'
'=' = '-...-'; '+' = '.-.-.'; '-' = '-....-'; '_' = '..--.-'; '"' = '.-..-.'; '@' = '.--.-.'
}
$result = @()
$Text = $Text.ToUpper()
foreach ($char in $Text.ToCharArray()) {
if ($morseDict.ContainsKey($char.ToString())) {
$result += $morseDict[$char.ToString()]
}
else {
Write-Warning "Character '$char' not found in Morse code dictionary"
}
}
return $result -join ' '
}
function Convert-MorseToText {
param (
[Parameter(Mandatory = $true)]
[string]$MorseCode
)
$textDict = @{
'.-' = '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'; '.----' = '1'; '..---' = '2'; '...--' = '3'; '....-' = '4'
'.....' = '5'; '-....' = '6'; '--...' = '7'; '---..' = '8'; '----.' = '9'; '-----' = '0'
'/' = ' '; '.-.-.-' = '.'; '--..--' = ','; '..--..' = '?'; '.----.' = "'"; '-.-.--' = '!'
'-..-.' = '/'; '-.--.' = '('; '-.--.-' = ')'; '.-...' = '&'; '---...' = ':'; '-.-.-.' = ';'
'-...-' = '='; '.-.-.' = '+'; '-....-' = '-'; '..--.-' = '_'; '.-..-.' = '"'; '.--.-.' = '@'
}
$words = $MorseCode -split ' '
$result = @()
foreach ($word in $words) {
$chars = $word -split ' '
$wordText = ''
foreach ($char in $chars) {
if ($char -and $textDict.ContainsKey($char.tostring())) {
$wordText += $textDict[$char.tostring()]
}
elseif ($char) {
Write-Warning "Morse sequence '$char' not recognized"
}
}
if ($wordText) {
$result += $wordText
}
}
return $result -join ' '
}
function Convert-MorseCode {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[string]$InputText,
[Parameter()]
[switch]$FromMorse
)
process {
if ($FromMorse) {
Convert-MorseToText -MorseCode $InputText
}
else {
Convert-TextToMorse -Text $InputText
}
}
}
@dfinke
Copy link
Author

dfinke commented Mar 15, 2025

'SOS' | Convert-MorseCode
... --- ...
'SOS' | Convert-MorseCode | Convert-MorseCode -FromMorse
SOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment