ParseUtil { public static byte[] hexStringToByteArray(String digits) { int len = digits.length(); if (!VALID_HEX.matcher(digits).matches() || (len & 0x1) != 0) { LOG.warn("Invalid hexadecimal string: {}", digits); return new byte[0]; } byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) (Character.digit(digits.charAt(i), 16) << 4 | Character.digit(digits.charAt(i + 1), 16)); } return data; } private  ParseUtil(); static long parseHertz(String hertz); static int parseLastInt(String s, int i); static long parseLastLong(String s, long li); static double parseLastDouble(String s, double d); static String parseLastString(String s); static String byteArrayToHexString(byte[] bytes); static byte[] hexStringToByteArray(String digits); static byte[] asciiStringToByteArray(String text, int length); static byte[] longToByteArray(long value, int valueSize, int length); static long strToLong(String str, int size); static long byteArrayToLong(byte[] bytes, int size); static float byteArrayToFloat(byte[] bytes, int size, int fpBits); static long unsignedIntToLong(int unsignedValue); static long unsignedLongToSignedLong(long unsignedValue); static String hexStringToString(String hexString); static int parseIntOrDefault(String s, int defaultInt); static long parseLongOrDefault(String s, long defaultLong); static long parseUnsignedLongOrDefault(String s, long defaultLong); static double parseDoubleOrDefault(String s, double defaultDouble); static long parseDHMSOrDefault(String s, long defaultLong); static String parseUuidOrDefault(String s, String defaultStr); static String getSingleQuoteStringValue(String line); static String getDoubleQuoteStringValue(String line); static String getStringBetween(String line, char c); static int getFirstIntValue(String line); static int getNthIntValue(String line, int n); static String removeMatchingString(final String original, final String toRemove); static long[] parseStringToLongArray(String s, int[] indices, int length, char delimiter); static int countStringToLongArray(String s, char delimiter); static String getTextBetweenStrings(String text, String before, String after); static long filetimeToUtcMs(long filetime, boolean local); static String parseMmDdYyyyToYyyyMmDD(String dateString); static OffsetDateTime parseCimDateTimeToOffset(String cimDateTime); static boolean filePathStartsWith(List<String> prefixList, String path); static long parseDecimalMemorySizeToBinary(String size); static Pair<String, String> parsePnPDeviceIdToVendorProductId(String pnpDeviceId); static long parseLshwResourceString(String resources); static Pair<String, String> parseLspciMachineReadable(String line); static long parseLspciMemorySize(String line); static List<Integer> parseHyphenatedIntList(String str); static String parseUtAddrV6toIP(int[] utAddrV6); static long hexStringToLong(String hexString, long defaultValue); static String removeLeadingDots(String dotPrefixedStr); static final Pattern whitespacesColonWhitespace; static final Pattern whitespaces; static final Pattern notDigits; static final Pattern startWithNotDigits; static final Pattern slash; }
@Test public void testHexStringToByteArray() { byte[] temp = { (byte) 0x12, (byte) 0xaf }; assertTrue(Arrays.equals(temp, ParseUtil.hexStringToByteArray("12af"))); assertEquals("12AF", ParseUtil.byteArrayToHexString(temp)); temp = new byte[0]; assertTrue(Arrays.equals(temp, ParseUtil.hexStringToByteArray("expected error abcde"))); assertTrue(Arrays.equals(temp, ParseUtil.hexStringToByteArray("abcde"))); }