Translating Text to Morse Code

by Feb 8, 2021

There seem to be web services for just about anything. Here is a web service that turns text to Morse code:

$Text = 'SOS This is an emergency!'

# URL-encode text
Add-Type -AssemblyName System.Web
$encoded = [System.Web.HttpUtility]::UrlEncode($Text)

# compose web service URL
$Url = "https://api.funtranslations.com/translate/morse.json?text=$encoded"

# call web service
(Invoke-RestMethod -UseBasicParsing -Uri $url).contents.translated

The result looks like this:

 
... --- ...     - .... .. ...     .. ...     .- -.     . -- . .-. --. . -. -.-. -.-- ---. 
 

If you’d really like to morse, parse the resulting text and create real beeps:

$Text = 'Happy New Year 2021!'

# URL-encode text
Add-Type -AssemblyName System.Web
$encoded = [System.Web.HttpUtility]::UrlEncode($Text)

# compose web service URL
$Url = "https://api.funtranslations.com/translate/morse.json?text=$encoded"

# call web service
$morse = (Invoke-RestMethod -UseBasicParsing -Uri $url).contents.translated

Foreach ($char in $morse.ToCharArray())
{
    switch ($char)
    {
        '.'      { [Console]::Beep(800, 300) }
        '-'      { [Console]::Beep(800, 900) }
        ' '      { Start-Sleep -Milliseconds 500 }
        default  { Write-Warning "Unknown char: $_"
                   [Console]::Beep(2000, 500) }

    }
    Write-Host $char -NoNewline
    Start-Sleep -Milliseconds 200
}
Write-Host "OK"


Twitter This Tip! ReTweet this Tip!