1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.ninjasoft.magiccodes.logic;
18
19 /***
20 * Perform various translations
21 *
22 * @author enigma
23 */
24 public class Translator {
25 /***
26 * Convert a byte array to a binary string
27 * @param c
28 * @return
29 */
30 public static String byteToBin(int[] in) {
31 StringBuffer result = new StringBuffer(in.length * 8);
32 for (int pos=0; pos < in.length; pos++) {
33 StringBuffer byteResult = new StringBuffer(8);
34 int mask = 1;
35 for (int i=0; i<8; i++) {
36 if ((in[pos] & mask) == mask)
37 byteResult.insert(0, "1");
38 else
39 byteResult.insert(0, "0");
40 mask = mask << 1;
41 }
42 result.append(byteResult);
43 }
44 return result.toString();
45 }
46
47 /***
48 * Convert a string of binary digits (0's and 1's) into a byte array
49 * @param bin
50 * @return
51 * @throws FormatException
52 */
53 public static int[] binToByte(String bin) throws FormatException {
54 bin = normalizeString(bin);
55 int len = bin.length() / 8;
56 int[] result = new int[len];
57 if (bin.length() % 8 != 0)
58 throw new FormatException("Length of binary data is not evenly divisible by 8!");
59 for (int charPos=0; charPos < len; charPos++) {
60 int value = 0;
61 for (int bitPos = 0; bitPos < 8; bitPos++) {
62 char c = bin.charAt((charPos * 8) + bitPos);
63
64 value *= 2;
65 if (c == '0')
66 value += 0;
67 else if (c == '1')
68 value += 1;
69 else
70 throw new FormatException("Non-binary character found in data: " + c);
71 }
72 result[charPos] = value;
73 }
74 return result;
75 }
76
77 /***
78 * Convert a byte array to a hex string
79 * @param in
80 * @return
81 */
82 public static String byteToHex(int[] in) {
83 StringBuffer result = new StringBuffer(in.length * 2);
84 for (int i=0; i<in.length; i++) {
85 String thisByte = Integer.toString(in[i], 16);
86 if (thisByte.length() == 1)
87 result.append('0');
88 result.append(thisByte.toUpperCase());
89 }
90 return result.toString();
91 }
92
93 /***
94 * Convert a hex string to a byte array
95 * @param in
96 * @return
97 * @throws FormatException
98 */
99 public static int[] hexToByte(String in) throws FormatException{
100 in = normalizeString(in);
101 int len = in.length() / 2;
102 int[] result = new int[len];
103 if (in.length() % 2 != 0)
104 throw new FormatException("The input hex string length is not even divisible by 2");
105 for (int charPos=0; charPos < len; charPos++) {
106 int value = 0;
107 for (int bitPos = 0; bitPos < 2; bitPos++) {
108 char c = Character.toUpperCase(in.charAt(charPos * 2 + bitPos));
109 if ((c >= '0') && (c <= '9')) {
110 value = value << 4;
111 value = value + c - '0';
112 }else if ((c >= 'A') && (c <= 'F')) {
113 value = value << 4;
114 value = value + c - 'A' + 10;
115 }else{
116 throw new FormatException("Non-hex character found in data: " + c);
117 }
118 }
119 result[charPos] = value;
120 }
121 return result;
122 }
123
124 /***
125 * Convert a byte array to ASCII
126 * @param in
127 * @return
128 */
129 public static String byteToAscii(int[] in) {
130 StringBuffer result = new StringBuffer(in.length);
131 for (int i=0; i<in.length; i++) {
132 result.append(String.valueOf((char) in[i]));
133 }
134 return result.toString();
135 }
136
137 /***
138 * Convert an ASCII string to a byte array
139 * @param in
140 * @return
141 */
142 public static int[] asciiToByte(String in) {
143 int[] result = new int[in.length()];
144 for (int i=0; i<in.length(); i++)
145 result[i] = in.charAt(i);
146 return result;
147 }
148
149 /***
150 * Convert a byte array to printable ASCII in HTML form (red question marks
151 * for unprintable characters)
152 * @param in
153 * @return
154 */
155 public static String byteToPrintableAscii(int[] in) {
156 StringBuffer result = new StringBuffer();
157 result.append("<html><head></head><body><code>");
158 for (int i=0; i<in.length; i++) {
159 char c = (char) in[i];
160 if ((c >= ' ') && (c <= '~'))
161 result.append(c);
162 else if (c == '\n')
163 result.append("<br>");
164 else if (c == '\r')
165 ;
166 else
167 result.append("<font color=#ff0000>?</font>");
168 }
169 result.append("</code></body></html>");
170 return result.toString();
171 }
172
173 /***
174 * Normalize a string by removing all whitespace, control characters, and
175 * high-ASCII
176 * @param in
177 * @return
178 */
179 public static String normalizeString(String in) {
180 StringBuffer result = new StringBuffer(in.length());
181 for (int i=0; i<in.length(); i++) {
182 char c = in.charAt(i);
183 if ((c > ' ') && (c <= '~'))
184 result.append(c);
185 }
186 return result.toString();
187 }
188 }