|
27 | 27 | public final class Numeric {
|
28 | 28 |
|
29 | 29 | private static final String HEX_PREFIX = "0x";
|
| 30 | + private static final char[] HEX_CHAR_MAP = "0123456789abcdef".toCharArray(); |
30 | 31 |
|
31 | 32 | private Numeric() {}
|
32 | 33 |
|
@@ -227,15 +228,18 @@ public static byte[] hexStringToByteArray(String input) {
|
227 | 228 | }
|
228 | 229 |
|
229 | 230 | public static String toHexString(byte[] input, int offset, int length, boolean withPrefix) {
|
230 |
| - StringBuilder stringBuilder = new StringBuilder(); |
231 |
| - if (withPrefix) { |
232 |
| - stringBuilder.append("0x"); |
233 |
| - } |
234 |
| - for (int i = offset; i < offset + length; i++) { |
235 |
| - stringBuilder.append(String.format("%02x", input[i] & 0xFF)); |
236 |
| - } |
| 231 | + final String output = new String(toHexCharArray(input, offset, length, withPrefix)); |
| 232 | + return withPrefix ? new StringBuilder(HEX_PREFIX).append(output).toString() : output; |
| 233 | + } |
237 | 234 |
|
238 |
| - return stringBuilder.toString(); |
| 235 | + private static char[] toHexCharArray(byte[] input, int offset, int length, boolean withPrefix) { |
| 236 | + final char[] output = new char[length << 1]; |
| 237 | + for (int i = offset, j = 0; i < length; i++, j++) { |
| 238 | + final int v = input[i] & 0xFF; |
| 239 | + output[j++] = HEX_CHAR_MAP[v >>> 4]; |
| 240 | + output[j] = HEX_CHAR_MAP[v & 0x0F]; |
| 241 | + } |
| 242 | + return output; |
239 | 243 | }
|
240 | 244 |
|
241 | 245 | public static String toHexString(byte[] input) {
|
|
0 commit comments