View Javadoc

1   package net.sf.mmapps.commons.util;
2   
3   import java.util.StringTokenizer;
4   
5   public class HexUtil {
6   
7      /***
8       * Converts a string of hex digits into a byte array of those digits
9       * @param hexString A String with ascii hex digits 
10      * @return byte[]
11      */
12     public static byte[] toByteArray(String hexString) {
13        byte[] number = new byte[hexString.length() / 2];
14        int i;
15        for (i = 0; i < hexString.length(); i += 2) {
16           int j = Integer.parseInt(hexString.substring(i, i + 2), 16);
17           number[i / 2] = (byte) (j & 0x000000ff);
18        }
19        return number;
20     }
21  
22     /***
23      * Converts a string of hex digits into a byte array of those digits
24      * @param hexString A String with ascii hex digits 
25      * @param separators A String containing the separator characters
26      * @return byte[]
27      */
28     public static byte[] toByteArray(String hexString, String separators) {
29        byte[] buf = new byte[hexString.length() / 3];
30  
31        int i = 0;
32        StringTokenizer values = new StringTokenizer(hexString, separators);
33        while (values.hasMoreTokens()) {
34           String s = values.nextToken();
35           int hex = Integer.parseInt(s, 16);
36           buf[i++] = (byte) (hex & 0x000000ff);
37        }
38        return buf;
39     }
40  
41     /***
42      * Convert int to Hex String
43      * @param i value to convert
44      * @return String
45      */
46     public static String toHex(int i) {
47        byte b[] = new byte[4];
48        b[0] = (byte) ((i & 0xff000000) >>> 24);
49        b[1] = (byte) ((i & 0x00ff0000) >>> 16);
50        b[2] = (byte) ((i & 0x0000ff00) >>> 8);
51        b[3] = (byte) ((i & 0x000000ff));
52  
53        return toHex(b[0]) + toHex(b[1]) + toHex(b[2]) + toHex(b[3]);
54     }
55  
56     /***
57      * Convert short to Hex String
58      * @param i value to convert
59      * @return String
60      */
61     public static String toHex(short i) {
62        byte b[] = new byte[2];
63        b[0] = (byte) ((i & 0xff00) >>> 8);
64        b[1] = (byte) ((i & 0x00ff));
65  
66        return toHex(b[0]) + toHex(b[1]);
67     }
68  
69     /***
70      * Convert byte to Hex String
71      * @param b value to convert
72      * @return String
73      */
74     public static String toHex(byte b) {
75        Integer iI = new Integer((b << 24) >>> 24);
76        int i = iI.intValue();
77  
78        if (i < (byte) 16) {
79           return "0" + Integer.toString(i, 16);
80        }
81        else {
82           return Integer.toString(i, 16);
83        }
84     }
85  
86     /***
87      * Convert byte array to Hex String
88      * @param b value to convert
89      * @return String
90      */
91     public static String toHex(byte[] b) {
92        return toHex(b, 0, b.length);
93     }
94  
95     /***
96      * Convert short array to Hex String
97      * @param b value to convert
98      * @return String
99      */
100    public static String toHex(short[] b) {
101       return toHex(b, 0, b.length);
102    }
103 
104    /***
105     * Convert int array to Hex String
106     * @param b value to convert
107     * @return String
108     */
109    public static String toHex(int[] b) {
110       return toHex(b, 0, b.length);
111    }
112 
113    /***
114     * Convert int array to Hex String
115     * @param b value to convert
116     * @param off offset
117     * @param len length
118     * @return String
119     */
120    public static String toHex(int[] b, int off, int len) {
121       if (b == null) {
122          return "";
123       }
124       StringBuffer s = new StringBuffer("");
125       int i;
126       for (i = off; i < len; i++) {
127          s.append(toHex(b[i]));
128       }
129       return s.toString();
130    }
131 
132    /***
133     * Convert short array to Hex String
134     * @param b value to convert
135     * @param off offset
136     * @param len length
137     * @return String
138     */
139    public static String toHex(short[] b, int off, int len) {
140       if (b == null) {
141          return "";
142       }
143       StringBuffer s = new StringBuffer("");
144       int i;
145       for (i = off; i < len; i++) {
146          s.append(toHex(b[i]));
147       }
148       return s.toString();
149    }
150 
151    /***
152     * Convert byte array to Hex String
153     * @param b value to convert
154     * @param off offset
155     * @param len length
156     * @return String
157     */
158    public static String toHex(byte[] b, int off, int len) {
159       if (b == null) {
160          return "";
161       }
162       StringBuffer s = new StringBuffer("");
163       int i;
164       for (i = off; i < len; i++) {
165          s.append(toHex(b[i]));
166       }
167       return s.toString();
168    }
169 
170    /***
171     * Convert int array to Hex format String
172     * @param b value to convert
173     * @return String
174     */
175    public static String toHexFormat(int[] b) {
176       return toHexFormat(b, 0, b.length);
177    }
178 
179    /***
180     * Convert short array to Hex format String
181     * @param b value to convert
182     * @return String
183     */
184    public static String toHexFormat(short[] b) {
185       return toHexFormat(b, 0, b.length);
186    }
187 
188    /***
189     * Convert byte array to Hex format String
190     * @param b value to convert
191     * @return String
192     */
193    public static String toHexFormat(byte[] b) {
194       return toHexFormat(b, 0, b.length);
195    }
196 
197    /***
198     * Convert byte array to Hex format String
199     * @param b value to convert
200     * @param off offset
201     * @param len length
202     * @return String
203     */
204    public static String toHexFormat(byte[] b, int off, int len) {
205       StringBuffer s = new StringBuffer("");
206       StringBuffer c = new StringBuffer("");
207       int i;
208 
209       if (b == null) {
210          return "<null>";
211       }
212 
213       for (i = off; i < len; i++) {
214          if (i % 16 == 0) {
215             s.append(toHex(i) + " ");
216          }
217 
218          s.append(" " + toHex(b[i]));
219          if (b[i] >= 0x20 && b[i] < 0x7f) {
220             c.append((char) b[i]);
221          }
222          else {
223             c.append('.');
224          }
225          if (i % 16 == 15) {
226             s.append(c).append("\n");
227             c.setLength(0);
228          }
229 
230       }
231 
232       if (i % 16 != 0) {
233          s.append(" \"").append(c).append("\"\n");
234       }
235 
236       return s.toString();
237    }
238 
239    /***
240     * Convert short array to Hex format String
241     * @param b value to convert
242     * @param off offset
243     * @param len length
244     * @return String
245     */
246    public static String toHexFormat(short[] b, int off, int len) {
247       StringBuffer s = new StringBuffer("");
248       int i;
249 
250       if (b == null) {
251          return "<null>";
252       }
253 
254       for (i = off; i < len; i++) {
255          s.append(" " + toHex(b[i]));
256          if (i % 16 == 7) {
257             s.append("\n");
258          }
259          else {
260             if (i % 4 == 3) {
261                s.append(" ");
262             }
263          }
264       }
265       if (i % 8 != 0) {
266          s.append("\n");
267       }
268 
269       return s.toString();
270    }
271 
272    /***
273     * Convert int array to Hex format String
274     * @param b value to convert
275     * @param off offset
276     * @param len length
277     * @return String
278     */
279    public static String toHexFormat(int[] b, int off, int len) {
280       StringBuffer s = new StringBuffer("");
281       int i;
282 
283       if (b == null) {
284          return "<null>";
285       }
286 
287       for (i = off; i < len; i++) {
288          s.append(" " + toHex(b[i]));
289          if (i % 4 == 3) {
290             s.append("\n");
291          }
292       }
293       if (i % 4 != 0) {
294          s.append("\n");
295       }
296       
297       return s.toString();
298    }
299 
300 }