1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.util;
36
37 import java.util.Random;
38
39 /***
40 * Various functions to encode and decode strings.
41 *
42 * @author Markus Eriksson
43 */
44 public final class StringUtil
45 {
46 private static final char[] RANDOM_CHARS = "abcdefghijklmnopqrstuvwxyz1234567890".toCharArray();
47
48 private static Random myRandom = new Random();
49
50 /***
51 * This class isn't intended to be instantiated.
52 */
53 private StringUtil()
54 {
55 }
56
57 /***
58 *
59 * @param stringTable
60 * @param text
61 * @return
62 */
63 public static int findString(String[] stringTable, String text)
64 {
65 if (stringTable != null)
66 {
67 for (int i = 0; i < stringTable.length; i++)
68 {
69 if ((stringTable[i] != null) && (stringTable[i].equals(text)))
70 {
71 return i;
72 }
73 }
74 }
75 return -1;
76 }
77
78 /***
79 * Converts a byte array to a string with hex values.
80 *
81 * @param theData
82 * Data to convert
83 * @return the encoded string
84 */
85 public static String bytesToHexString(byte[] theData)
86 {
87 StringBuffer hexStrBuff = new StringBuffer(theData.length * 2);
88
89 for (int i = 0; i < theData.length; i++)
90 {
91 String hexByteStr = Integer.toHexString(theData[i] & 0xff).toUpperCase();
92 if (hexByteStr.length() == 1)
93 {
94 hexStrBuff.append("0");
95 }
96 hexStrBuff.append(hexByteStr);
97 }
98
99 return hexStrBuff.toString();
100 }
101
102 /***
103 * Converts a byte to a string with hex values.
104 *
105 * @param theByte
106 * Byte to convert
107 * @return the encoded string
108 */
109 public static String byteToHexString(byte theByte)
110 {
111 StringBuffer hexStrBuff = new StringBuffer(2);
112
113 String hexByteStr = Integer.toHexString(theByte & 0xff).toUpperCase();
114 if (hexByteStr.length() == 1)
115 {
116 hexStrBuff.append("0");
117 }
118 hexStrBuff.append(hexByteStr);
119
120 return hexStrBuff.toString();
121 }
122
123 /***
124 * Converts a string of hex characters to a byte array.
125 *
126 * @param theHexString
127 * The hex string to read
128 * @return the resulting byte array
129 */
130 public static byte[] hexStringToBytes(String theHexString)
131 {
132 byte[] data = new byte[theHexString.length() / 2];
133
134 for (int i = 0; i < data.length; i++)
135 {
136 String a = theHexString.substring(i * 2, i * 2 + 2);
137 data[i] = (byte) Integer.parseInt(a, 16);
138 }
139
140 return data;
141 }
142
143 /***
144 * Method intToString.
145 *
146 * Converst an integer to nChars characters
147 *
148 * @param value
149 * Integer value
150 * @param nChars
151 * Number of chars to represent the "value"
152 * @return String The string representing "value"
153 */
154 public static String intToString(int value, int nChars)
155 {
156 String strValue = Integer.toString(value);
157 StringBuffer strBuf = new StringBuffer(nChars);
158
159 for (int i = strValue.length(); i < nChars; i++)
160 {
161 strBuf.append('0');
162 }
163 strBuf.append(strValue);
164
165 return strBuf.toString();
166 }
167
168 /***
169 * Generates a random string of the given length.
170 *
171 * "abcdefghijklmnopqrstuvwxyz1234567890"
172 *
173 * @param length
174 * @return A random string
175 */
176 public static String randString(int length)
177 {
178 StringBuffer sb = new StringBuffer(length);
179
180 for (int i = 0; i < length; i++)
181 {
182 sb.append(RANDOM_CHARS[myRandom.nextInt(RANDOM_CHARS.length)]);
183 }
184
185 return sb.toString();
186 }
187 }