View Javadoc

1   //////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003  Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  }