Skip to content

Instantly share code, notes, and snippets.

@shkschneider
Last active April 28, 2016 14:19
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 shkschneider/6e0251b2d05a410e446cb22d2d8a749f to your computer and use it in GitHub Desktop.
Save shkschneider/6e0251b2d05a410e446cb22d2d8a749f to your computer and use it in GitHub Desktop.
Code39Extended
public class Code39Extended {
protected Code39Extended() {
// Empty
}
private static final Character NULL = '\0';
// <https://en.wikipedia.org/wiki/Code_39#Full_ASCII_Code_39>
private static final HashMap<String, HashMap<String, Character>> EXTENDED = new HashMap<String, HashMap<String, Character>>() {
{
put("$", new HashMap<String, Character>() {
{
put("A", NULL); // 1
put("B", NULL); // 2
put("C", NULL); // 3
put("D", NULL); // 4
put("E", NULL); // 5
put("F", NULL); // 6
put("G", NULL); // 7
put("H", NULL); // 8
put("I", NULL); // 9
put("J", NULL); // 10
put("K", NULL); // 11
put("L", NULL); // 12
put("M", NULL); // 13
put("N", NULL); // 14
put("O", NULL); // 15
put("P", NULL); // 16
put("Q", NULL); // 17
put("R", NULL); // 18
put("S", NULL); // 19
put("T", NULL); // 20
put("U", NULL); // 21
put("V", NULL); // 22
put("W", NULL); // 23
put("X", NULL); // 24
put("Y", NULL); // 25
put("Z", NULL); // 26
}
});
put("%", new HashMap<String, Character>() {
{
put("A", NULL); // 27
put("B", NULL); // 28
put("C", NULL); // 29
put("D", NULL); // 30
put("E", NULL); // 31
put("F", ';'); // 59
put("G", '<'); // 60
put("H", '='); // 61
put("I", '>'); // 62
put("J", '?'); // 63
put("K", '['); // 91
put("L", '\\'); // 92
put("M", ']'); // 93
put("N", '^'); // 94
put("O", '_'); // 95
put("P", '{'); // 123
put("Q", '|'); // 124
put("R", '}'); // 125
put("S", '~'); // 126
put("T", NULL); // 127
put("U", NULL); // 0
put("V", '@'); // 64
put("W", '`'); // 96
put("X", NULL); // 127
put("Y", NULL); // 127
put("Z", NULL); // 127
}
});
put("/", new HashMap<String, Character>() {
{
put("A", '!'); // 33
put("B", '"'); // 34
put("C", '#'); // 35
put("D", '$'); // 36
put("E", '%'); // 37
put("F", '&'); // 38
put("G", '\''); // 39
put("H", '('); // 40
put("I", ')'); // 41
put("J", '*'); // 42
put("K", '+'); // 43
put("L", ','); // 44
put("O", '/'); // 45
}
});
put("+", new HashMap<String, Character>() {
{
put("A", 'a'); // 97
put("B", 'b'); // 98
put("C", 'c'); // 99
put("D", 'd'); // 100
put("E", 'e'); // 101
put("F", 'f'); // 102
put("G", 'g'); // 103
put("H", 'h'); // 104
put("I", 'i'); // 105
put("J", 'j'); // 106
put("K", 'k'); // 107
put("L", 'l'); // 108
put("M", 'm'); // 109
put("N", 'n'); // 110
put("O", 'o'); // 111
put("P", 'p'); // 112
put("Q", 'q'); // 113
put("R", 'r'); // 114
put("S", 's'); // 115
put("T", 't'); // 116
put("U", 'u'); // 117
put("V", 'v'); // 118
put("W", 'w'); // 119
put("X", 'x'); // 120
put("Y", 'y'); // 121
put("Z", 'z'); // 122
}
});
}
};
public static String decode(@NonNull final String ascii) {
String extended = "";
for (int i = 0; i < ascii.length(); i++) {
final String ss = String.valueOf(ascii.charAt(i));
if (EXTENDED.containsKey(ss)) {
final Map<String, Character> map = EXTENDED.get(ss);
final String s = String.valueOf(ascii.charAt(i + 1));
if (map.containsKey(s)) {
final Character c = map.get(s);
if (c != NULL) {
extended += String.valueOf(c);
}
i++;
continue;
}
}
extended += ss;
}
return extended;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment