slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 1 | package org.onosproject.xran.codecs.util; |
| 2 | |
| 3 | public class HexConverter { |
| 4 | |
| 5 | public static String toHexString(byte b) { |
| 6 | StringBuilder builder = new StringBuilder(); |
| 7 | appendHexString(b, builder); |
| 8 | return builder.toString(); |
| 9 | } |
| 10 | |
| 11 | public static String toHexString(byte[] bytes) { |
| 12 | return toHexString(bytes, 0, bytes.length); |
| 13 | } |
| 14 | |
| 15 | public static String toHexString(byte[] bytes, int offset, int length) { |
| 16 | StringBuilder builder = new StringBuilder(); |
| 17 | |
| 18 | int l = 1; |
| 19 | for (int i = offset; i < (offset + length); i++) { |
| 20 | if ((l != 1) && ((l - 1) % 8 == 0)) { |
| 21 | builder.append(' '); |
| 22 | } |
| 23 | if ((l != 1) && ((l - 1) % 16 == 0)) { |
| 24 | builder.append('\n'); |
| 25 | } |
| 26 | l++; |
| 27 | appendHexString(bytes[i], builder); |
| 28 | if (i != offset + length - 1) { |
| 29 | builder.append(' '); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return builder.toString(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns the integer value as hex string filled with leading zeros. If you do not want leading zeros use |
| 38 | * Integer.toHexString(int i) instead. |
| 39 | * |
| 40 | * @param i |
| 41 | * the integer value to be converted |
| 42 | * @return the hex string |
| 43 | */ |
| 44 | public static String toShortHexString(int i) { |
| 45 | byte[] bytes = new byte[] { (byte) (i >> 24), (byte) (i >> 16), (byte) (i >> 8), (byte) (i) }; |
| 46 | return toShortHexString(bytes); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the long value as hex string filled with leading zeros. If you do not want leading zeros use |
| 51 | * Long.toHexString(long i) instead. |
| 52 | * |
| 53 | * @param l |
| 54 | * the long value to be converted |
| 55 | * @return the hex string |
| 56 | */ |
| 57 | public static String toShortHexString(long l) { |
| 58 | byte[] bytes = new byte[] { (byte) (l >> 56), (byte) (l >> 48), (byte) (l >> 40), (byte) (l >> 32), |
| 59 | (byte) (l >> 24), (byte) (l >> 16), (byte) (l >> 8), (byte) (l) }; |
| 60 | return toShortHexString(bytes); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the byte as a hex string. If b is less than 16 the hex string returned contains a leading zero. |
| 65 | * |
| 66 | * @param b |
| 67 | * the byte to be converted |
| 68 | * @return the byte as a hex string. |
| 69 | */ |
| 70 | public static String toShortHexString(byte b) { |
| 71 | return toShortHexString(new byte[] { b }); |
| 72 | } |
| 73 | |
| 74 | private final static char[] hexArray = "0123456789ABCDEF".toCharArray(); |
| 75 | |
| 76 | public static String toShortHexString(byte[] bytes) { |
| 77 | return toShortHexString(bytes, 0, bytes.length); |
| 78 | } |
| 79 | |
| 80 | public static String toShortHexString(byte[] bytes, int offset, int length) { |
| 81 | char[] hexChars = new char[length * 2]; |
| 82 | for (int j = offset; j < (offset + length); j++) { |
| 83 | int v = bytes[j] & 0xff; |
| 84 | hexChars[j * 2] = hexArray[v >>> 4]; |
| 85 | hexChars[j * 2 + 1] = hexArray[v & 0x0f]; |
| 86 | } |
| 87 | return new String(hexChars); |
| 88 | } |
| 89 | |
| 90 | public static byte[] fromShortHexString(String shortHexString) throws NumberFormatException { |
| 91 | |
| 92 | validate(shortHexString); |
| 93 | |
| 94 | int length = shortHexString.length(); |
| 95 | |
| 96 | byte[] data = new byte[length / 2]; |
| 97 | for (int i = 0; i < length; i += 2) { |
| 98 | int firstCharacter = Character.digit(shortHexString.charAt(i), 16); |
| 99 | int secondCharacter = Character.digit(shortHexString.charAt(i + 1), 16); |
| 100 | |
| 101 | if (firstCharacter == -1 || secondCharacter == -1) { |
| 102 | throw new NumberFormatException("string is not a legal hex string."); |
| 103 | } |
| 104 | |
| 105 | data[i / 2] = (byte) ((firstCharacter << 4) + secondCharacter); |
| 106 | } |
| 107 | return data; |
| 108 | } |
| 109 | |
| 110 | public static void appendShortHexString(byte b, StringBuilder builder) { |
| 111 | builder.append(toShortHexString(b)); |
| 112 | } |
| 113 | |
| 114 | public static void appendShortHexString(StringBuilder builder, byte[] bytes, int offset, int length) { |
| 115 | builder.append(toShortHexString(bytes, offset, length)); |
| 116 | } |
| 117 | |
| 118 | public static void appendHexString(byte b, StringBuilder builder) { |
| 119 | builder.append("0x"); |
| 120 | appendShortHexString(b, builder); |
| 121 | } |
| 122 | |
| 123 | public static void appendHexString(StringBuilder builder, byte[] byteArray, int offset, int length) { |
| 124 | int l = 1; |
| 125 | for (int i = offset; i < (offset + length); i++) { |
| 126 | if ((l != 1) && ((l - 1) % 8 == 0)) { |
| 127 | builder.append(' '); |
| 128 | } |
| 129 | if ((l != 1) && ((l - 1) % 16 == 0)) { |
| 130 | builder.append('\n'); |
| 131 | } |
| 132 | l++; |
| 133 | appendHexString(byteArray[i], builder); |
| 134 | if (i != offset + length - 1) { |
| 135 | builder.append(' '); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private static void validate(String s) { |
| 141 | if (s == null) { |
| 142 | throw new IllegalArgumentException("string s may not be null"); |
| 143 | } |
| 144 | |
| 145 | if ((s.length() == 0) || ((s.length() % 2) != 0)) { |
| 146 | throw new NumberFormatException("string is not a legal hex string."); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Don't let anyone instantiate this class. |
| 152 | */ |
| 153 | private HexConverter() { |
| 154 | } |
| 155 | } |