1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.dexterim.codec;
20
21 import java.io.ByteArrayOutputStream;
22 import java.util.StringTokenizer;
23
24 import org.apache.commons.codec.binary.Hex;
25
26 /***
27 * @author christoph
28 * TODO Document
29 */
30 public class HexPrinter {
31
32 private HexPrinter() {
33
34 }
35
36 public static String formatNoSpace(byte[] data) {
37 return new String(Hex.encodeHex(data));
38 }
39
40 public static String format(byte[] data) {
41 return format(Hex.encodeHex(data));
42 }
43
44 private static String format(char[] data) {
45 StringBuffer returnValue = new StringBuffer();
46
47 for (int i = 0; i < data.length; i += 2) {
48 returnValue.append(data[i]);
49 returnValue.append(data[i + 1]);
50
51 if (i < data.length - 2) {
52 returnValue.append(" ");
53 }
54 }
55
56 return returnValue.toString();
57 }
58
59 public static String convertString(String hexString) {
60
61 StringTokenizer tokenizer = new StringTokenizer(hexString);
62
63 ByteArrayOutputStream out = new ByteArrayOutputStream();
64
65 while (tokenizer.hasMoreTokens()) {
66 String token = tokenizer.nextToken();
67
68 out.write(Integer.parseInt(token, 16));
69 }
70
71 return new String(out.toByteArray());
72 }
73
74 /***
75 * @param string
76 * @return
77 */
78 public static String convertNumber(String hexString) {
79 StringTokenizer tokenizer = new StringTokenizer(hexString);
80
81 StringBuffer returnValue = new StringBuffer();
82
83 while (tokenizer.hasMoreTokens()) {
84 String token = tokenizer.nextToken();
85
86 returnValue.append(Integer.parseInt(token, 16));
87
88 if (tokenizer.hasMoreElements()) {
89 returnValue.append(" ");
90 }
91 }
92
93 return returnValue.toString();
94 }
95 }