/** * Parses a 16 character lower-hex string with no prefix into an unsigned long, starting at the * spe index. */ static long lowerHexToUnsignedLong(String lowerHex, int index) { int length = lowerHex.length() - index; if (length < 1) throw new NumberFormatException("Nothing to parse at index " + index); long result = 0; for (int endIndex = Math.min(index + 16, lowerHex.length()); index < endIndex; index++) { char c = lowerHex.charAt(index); result <<= 4; if (c >= '0' && c <= '9') { result |= c - '0'; } else if (c >= 'a' && c <= 'f') { result |= c - 'a' + 10; } else { throw isntLowerHexLong(lowerHex); } } return result; }
/** * Parses a 1 to 32 character lower-hex string with no prefix into an unsigned long, tossing any * bits higher than 64. */ public static long lowerHexToUnsignedLong(String lowerHex) { int length = lowerHex.length(); if (length < 1 || length > 32) throw isntLowerHexLong(lowerHex); // trim off any high bits int beginIndex = length > 16 ? length - 16 : 0; return lowerHexToUnsignedLong(lowerHex, beginIndex); }
/** Inspired by {@code okio.Buffer.writeLong} */ static String toLowerHex(long v) { char[] data = new char[16]; writeHexLong(data, 0, v); return new String(data); }
/** Inspired by {@code okio.Buffer.writeLong} */ static void writeHexLong(char[] data, int pos, long v) { writeHexByte(data, pos + 0, (byte) ((v >>> 56L) & 0xff)); writeHexByte(data, pos + 2, (byte) ((v >>> 48L) & 0xff)); writeHexByte(data, pos + 4, (byte) ((v >>> 40L) & 0xff)); writeHexByte(data, pos + 6, (byte) ((v >>> 32L) & 0xff)); writeHexByte(data, pos + 8, (byte) ((v >>> 24L) & 0xff)); writeHexByte(data, pos + 10, (byte) ((v >>> 16L) & 0xff)); writeHexByte(data, pos + 12, (byte) ((v >>> 8L) & 0xff)); writeHexByte(data, pos + 14, (byte) (v & 0xff)); }
/** * @param hexString The lowercase hexadecimal string representing an unsigned 64-bit long that you want to convert to a Java long primitive. * @param index index to read 16 hexadecimal characters from * @return The Java long primitive represented by the given lowercase hex string. If the string isn't lowercase hexadecimal encoded then a * {@link NumberFormatException} will be thrown. */ public static long unsignedLowerHexStringToLong(String hexString, int index) { return ZipkinHexHelpers.lowerHexToUnsignedLong(hexString, index); }
/** * @param primitiveLong The long value that should be converted to an unsigned long encoded in a hexadecimal string. * @return The given long value converted to an unsigned hex encoded string of length 16 (zeroes will be prepended as padding if necessary to * reach a string length of 16). You can convert back to a Java long primitive by passing the result into * {@link #unsignedLowerHexStringToLong(String)}. */ public static String longToUnsignedLowerHexString(long primitiveLong) { return ZipkinHexHelpers.toLowerHex(primitiveLong); }
/** * @param hexString The lowercase hexadecimal string representing an unsigned 64-bit long that you want to convert to a Java long primitive. * @return The Java long primitive represented by the given lowercase hex string. If the string isn't lowercase hexadecimal encoded then a * {@link NumberFormatException} will be thrown. If the string is larger than 64-bits, any higher bits will be ignored. */ public static long unsignedLowerHexStringToLong(String hexString) { return ZipkinHexHelpers.lowerHexToUnsignedLong(hexString); }