Skip to content

Instantly share code, notes, and snippets.

@MrPowerGamerBR
Created July 18, 2017 11:33
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 MrPowerGamerBR/a220e1574bcef5920ff650649124efef to your computer and use it in GitHub Desktop.
Save MrPowerGamerBR/a220e1574bcef5920ff650649124efef to your computer and use it in GitHub Desktop.
package com.mrpowergamerbr.loritta.utils
val morseValues = mapOf(
// ALFABETO
'A' to ".-",
'B' to "-...",
'C' to "-.-.",
'D' to "-..",
'E' to ".",
'F' to "..-.",
'G' to "--.",
'H' to "....",
'I' to "..",
'J' to ".---",
'K' to "-.-",
'L' to ".-..",
'M' to "--",
'N' to "-.",
'O' to "---",
'P' to ".--.",
'Q' to "--.-",
'R' to ".-.",
'S' to "...",
'T' to "-",
'U' to "..-",
'V' to "...-",
'W' to ".--",
'X' to "-..-",
'Y' to "-.--",
'Z' to "--..",
// NÚMEROS
'1' to "·----",
'2' to "··---",
'3' to "···--",
'4' to "····-",
'5' to "·····",
'6' to "-····",
'7' to "--···",
'8' to "---··",
'9' to "----·",
'0' to "-----",
// PONTUAÇÕES COMUNS
'.' to "·-·-·-",
',' to "--··--",
'?' to "··--··",
'\'' to "·----·",
'!' to "-·-·--",
'/' to "-··-·",
'(' to "-·--·",
')' to "-·--·-",
'&' to "·-···",
':' to "---···",
';' to "-·-·-·",
'=' to "-···-",
'-' to "-····-",
'_' to "··--·-",
'"' to "·-··-·",
'$' to "···-··-",
'@' to "·--·-·",
' ' to "/",
// OUTROS CARACTERES
'ä' to "·-·-",
'à' to "·--·-",
'ç' to "-·-··",
'ð' to "··--·",
'è' to "·-··-",
'é' to "··-··",
'ĝ' to "--·-·",
'ĥ' to "-·--·",
'ĵ' to "·---·",
'ñ' to "--·--",
'ö' to "---·",
'ŝ' to "···-·",
'þ' to "·--··",
'ü' to "··--"
)
fun String.fromMorse(): String {
// Criar uma string vazia para guardar a nossa mensagem em texto comum
var text = "";
// Separar nossa string em morse em espaços para fazer um for nela
this.split(" ").forEach { inMorse ->
// Pegar o valor do char em morse
val inTextEntry = morseValues.entries.firstOrNull { it.value.equals(inMorse) }
if (inTextEntry != null) { // E, caso seja diferente de null...
text += inTextEntry.key // Pegar o nosso valor e colocar na nossa string!
}
}
return text
}
fun String.toMorse(): String {
// Criar uma string vazia para guardar a nossa mensagem em morse
var morse = "";
// Fazer um for na nossa mensagem
this.toCharArray().forEach { char ->
// Pegar o valor do char em morse
val inMorse = morseValues[char]
if (inMorse != null) { // E, caso seja diferente de null...
morse += "$inMorse " // Pegar o nosso valor e colocar na nossa string!
}
}
return morse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment