import java.math.BigInteger;
import java.util.HashMap;
public class XMorse {
private static final HashMap<String, String> p = new HashMap<>();
private static final HashMap<String, String> u = new HashMap<>();
static {
p.put("A", "01");
p.put("B", "1000");
p.put("C", "1010");
p.put("D", "100");
p.put("E", "0");
p.put("F", "0010");
p.put("G", "110");
p.put("H", "0000");
p.put("I", "00");
p.put("J", "0111");
p.put("K", "101");
p.put("L", "0100");
p.put("M", "11");
p.put("N", "10");
p.put("O", "111");
p.put("P", "0110");
p.put("Q", "1101");
p.put("R", "010");
p.put("S", "000");
p.put("T", "1");
p.put("U", "001");
p.put("V", "0001");
p.put("W", "011");
p.put("X", "1001");
p.put("Y", "1011");
p.put("Z", "1100");
p.put("0", "11111");
p.put("1", "01111");
p.put("2", "00111");
p.put("3", "00011");
p.put("4", "00001");
p.put("5", "00000");
p.put("6", "10000");
p.put("7", "11000");
p.put("8", "11100");
p.put("9", "11110");
p.put(".", "010101");
p.put(",", "110011");
p.put("?", "001100");
p.put("'", "011110");
p.put("!", "101011");
p.put("/", "10010");
p.put("(", "10110");
p.put(")", "101101");
p.put("&", "01000");
p.put(":", "111000");
p.put(";", "101010");
p.put("=", "10001");
p.put("+", "01010");
p.put("-", "100001");
p.put("_", "001101");
p.put("\"", "010010");
p.put("$", "0001001");
p.put("@", "011010");
for (String key : p.keySet()) {
u.put(p.get(key), key);
}
}
public static String encode(String input, String space, String shortChar, String longChar) {
StringBuilder result = new StringBuilder();
String[] symbols = new String[]{space, shortChar, longChar};
input = input.replace(" ", "").toUpperCase();
for (int i = 0; i < input.length(); i++) {
String character = String.valueOf(input.charAt(i));
String symbol = p.get(character);
if (symbol == null) {
symbol = toMorseCode(character);
}
symbol = symbol.replace("0", symbols[1]).replace("1", symbols[2]);
result.append(symbol);
result.append(symbols[0]);
}
return result.toString();
}
private static String toMorseCode(String input) {
StringBuilder r = new StringBuilder();
for (int n = 0; n < input.length(); n++) {
String hex = Integer.toHexString(input.charAt(n));
String padded = ("0000" + hex).substring(hex.length());
r.append(padded);
}
String binaryString = new BigInteger(r.toString(), 16).toString(2);
return binaryString;
}
public static String decode(String input, String space, String shortChar, String longChar) {
StringBuilder result = new StringBuilder();
String[] symbols = new String[]{space, shortChar, longChar};
String[] parts = input.split(space);
for (String part : parts) {
String symbol = part.replace(shortChar, "0").replace(longChar, "1");
String character = u.get(symbol);
if (character == null) {
character = fromMorseCode(symbol);
}
result.append(character);
}
return result.toString();
}
private static String fromMorseCode(String input) {
int decimal = Integer.parseInt(input, 2);
return String.valueOf((char) decimal);
}
public void main(String[] args) {
String encoded = XMorse.encode("HELLO", "/", ".", "-");
System.out.println(encoded); // .... . .-.. .-.. --- / .-- --- .-. .-.. -..
String decoded = XMorse.decode(".... . .-.. .-.. --- / .-- --- .-. .-.. -..", "/", ".", "-");
System.out.println(decoded); // HELLO WORLD
}
}