Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active November 1, 2016 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/816bf97efd61b8f9793a29e92dfd9eaf to your computer and use it in GitHub Desktop.
Save caelifer/816bf97efd61b8f9793a29e92dfd9eaf to your computer and use it in GitHub Desktop.
package main
import (
"encoding/base64"
"fmt"
"log"
)
type MorseChar string
func (mc MorseChar) Decode() []byte {
if r, ok := morseCode2Littera[mc]; ok {
return []byte{r}
}
return []byte(mc)
}
var morseCode2Littera = map[MorseChar]byte{
".-": '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',
}
var edata = "MzkzMzUzNTM5NTMzMzk1Mzc5OTUzNzMzMzM1MzUzOTM1Mw=="
func main() {
// Convert from base64
sdata, err := base64.StdEncoding.DecodeString(edata)
if err != nil {
log.Fatal(err)
}
code := make([]byte, 0, 4)
var morseCode []byte
var decodedMsg []byte
for _, c := range sdata {
switch c {
case '3':
code = append(code, '.')
continue
case '9':
code = append(code, '-')
continue
case '7':
mc := MorseChar(code)
morseCode = append(morseCode, []byte(mc)...)
morseCode = append(morseCode, []byte(" ")...)
decodedMsg = append(decodedMsg, mc.Decode()...)
decodedMsg = append(decodedMsg, ' ')
case '5':
mc := MorseChar(code)
morseCode = append(morseCode, []byte(mc)...)
morseCode = append(morseCode, ' ')
decodedMsg = append(decodedMsg, mc.Decode()...)
}
code = code[:0]
}
// Print final letter
if len(code) > 0 {
mc := MorseChar(code)
morseCode = append(morseCode, []byte(mc)...)
decodedMsg = append(decodedMsg, mc.Decode()...)
}
// Final output
fmt.Println(string(morseCode))
fmt.Println(string(decodedMsg))
}
@caelifer
Copy link
Author

caelifer commented Jul 19, 2016

From the article here - https://habrahabr.ru/post/305782/

Live code - https://play.golang.org/p/QQCQdgAdqI

Output:

.-.. . .- ...- .   -- .   .... . .-. .
LEAVE ME HERE

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