|
1 | 1 | package org.firebirdsql.decimal.util; |
2 | 2 |
|
| 3 | +import java.util.HexFormat; |
| 4 | + |
3 | 5 | /** |
4 | 6 | * Helper methods for byte arrays. |
5 | 7 | * |
6 | | - * @author <a href="mailto:mrotteveel@users.sourceforge.net">Mark Rotteveel</a> |
7 | | - * @since 3.0 |
| 8 | + * @author Mark Rotteveel |
8 | 9 | */ |
9 | 10 | public final class ByteArrayHelper { |
10 | 11 |
|
11 | | - //@formatter:off |
12 | | - private static final char[] BYTE_2_HEX=( |
13 | | - "000102030405060708090A0B0C0D0E0F"+ |
14 | | - "101112131415161718191A1B1C1D1E1F"+ |
15 | | - "202122232425262728292A2B2C2D2E2F"+ |
16 | | - "303132333435363738393A3B3C3D3E3F"+ |
17 | | - "404142434445464748494A4B4C4D4E4F"+ |
18 | | - "505152535455565758595A5B5C5D5E5F"+ |
19 | | - "606162636465666768696A6B6C6D6E6F"+ |
20 | | - "707172737475767778797A7B7C7D7E7F"+ |
21 | | - "808182838485868788898A8B8C8D8E8F"+ |
22 | | - "909192939495969798999A9B9C9D9E9F"+ |
23 | | - "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"+ |
24 | | - "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"+ |
25 | | - "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"+ |
26 | | - "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"+ |
27 | | - "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"+ |
28 | | - "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF").toCharArray(); |
29 | | - //@formatter:on |
30 | | - |
31 | 12 | private ByteArrayHelper() { |
32 | 13 | // no instances |
33 | 14 | } |
34 | 15 |
|
35 | | - /** |
36 | | - * Converts the provided byte array to a hexadecimal string |
37 | | - * <p> |
38 | | - * Adapted from http://stackoverflow.com/a/21429909/466862 by <a href="http://stackoverflow.com/users/1182868/higginse">higginse</a> |
39 | | - * </p> |
40 | | - * |
41 | | - * @param bytes byte array (not {@code null} |
42 | | - * @return String with the content of the byte array in hexadecimal. |
43 | | - */ |
44 | | - public static String toHexString(byte[] bytes) { |
45 | | - final int length = bytes.length; |
46 | | - final char[] chars = new char[length << 1]; |
47 | | - final char[] byte2Hex = BYTE_2_HEX; |
48 | | - int index = 0; |
49 | | - int offset = 0; |
50 | | - while (offset < length) { |
51 | | - int hexIndex = (bytes[offset++] & 0xFF) << 1; |
52 | | - chars[index++] = byte2Hex[hexIndex++]; |
53 | | - chars[index++] = byte2Hex[hexIndex]; |
54 | | - } |
55 | | - return new String(chars); |
56 | | - } |
57 | | - |
58 | 16 | /** |
59 | 17 | * Converts a hexadecimal string to a byte array. |
60 | | - * <p> |
61 | | - * Adapted from https://stackoverflow.com/a/140861/466862 by <a href="https://stackoverflow.com/users/3093/dave-l">Dave L.</a> |
62 | | - * </p> |
63 | 18 | * |
64 | | - * @param hexString Hexadecimal string |
65 | | - * @return byte array |
| 19 | + * @param hexString |
| 20 | + * Hexadecimal string or {@code null} |
| 21 | + * @return byte array, or {@code null} if {@code hexString} is {@code null} |
66 | 22 | */ |
67 | 23 | public static byte[] hexToBytes(String hexString) { |
68 | 24 | if (hexString == null) { |
69 | 25 | return null; |
70 | 26 | } |
71 | | - int len = hexString.length(); |
72 | | - byte[] data = new byte[len / 2]; |
73 | | - for (int i = 0; i < len; i += 2) { |
74 | | - data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) |
75 | | - + Character.digit(hexString.charAt(i+1), 16)); |
76 | | - } |
77 | | - return data; |
| 27 | + return HexFormat.of().parseHex(hexString); |
78 | 28 | } |
| 29 | + |
79 | 30 | } |
0 commit comments