/** * Converts a digit into an lower-case hexadecimal character or the null character if invalid. * * @param digit a number 0 - 15 * @return the hex character for that digit or '\0' if invalid */ public static char getLowerCaseHex(final int digit) { if (digit < 0 || digit >= 16) { return '\0'; } return digit < 10 ? getNumericalDigit(digit) : getLowerCaseAlphaDigit(digit); }
/** * Converts a digit into an upper-case hexadecimal character or the null character if invalid. * * @param digit a number 0 - 15 * @return the hex character for that digit or '\0' if invalid */ public static char getUpperCaseHex(final int digit) { if (digit < 0 || digit >= 16) { return '\0'; } return digit < 10 ? getNumericalDigit(digit) : getUpperCaseAlphaDigit(digit); }
@Test public void invalidDigitReturnsNullCharacter() throws Exception { assertEquals('\0', Chars.getUpperCaseHex(-1)); assertEquals('\0', Chars.getUpperCaseHex(16)); assertEquals('\0', Chars.getUpperCaseHex(400)); assertEquals('\0', Chars.getLowerCaseHex(-1)); assertEquals('\0', Chars.getLowerCaseHex(16)); assertEquals('\0', Chars.getLowerCaseHex(400)); }
if (Character.isISOControl(c)) { toAppendTo.setCharAt(lastPos--, Chars.getUpperCaseHex(c & 0xF)); toAppendTo.setCharAt(lastPos--, Chars.getUpperCaseHex((c & 0xF0) >> 4)); toAppendTo.setCharAt(lastPos--, '0'); toAppendTo.setCharAt(lastPos--, '0');
@Test public void validDigitReturnsProperCharacter() throws Exception { final char[] expectedLower = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; final char[] expectedUpper = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; for (int i = 0; i < 16; i++) { assertEquals(String.format("Expected %x", i), expectedLower[i], Chars.getLowerCaseHex(i)); assertEquals(String.format("Expected %X", i), expectedUpper[i], Chars.getUpperCaseHex(i)); } } }
@Override void escape(final StringBuilder toAppendTo, final int start) { for (int i = toAppendTo.length() - 1; i >= start; i--) { // backwards: length may change final char c = toAppendTo.charAt(i); if (Character.isISOControl(c)) { // all iso control characters are in U+00xx toAppendTo.setCharAt(i, '\\'); toAppendTo.insert(i + 1, "u0000"); toAppendTo.setCharAt(i + 4, Chars.getUpperCaseHex((c & 0xF0) >> 4)); toAppendTo.setCharAt(i + 5, Chars.getUpperCaseHex(c & 0xF)); } else if (c == '"' || c == '\\') { // only " and \ need to be escaped; other escapes are optional toAppendTo.insert(i, '\\'); } } } };
/** * Converts a digit into an uppercase hexadecimal character or the null character if invalid. * * @param digit a number 0 - 15 * @return the hex character for that digit or '\0' if invalid */ public static char getUpperCaseHex(final int digit) { if (digit < 0 || digit >= 16) { return '\0'; } return digit < 10 ? getNumericalDigit(digit) : getUpperCaseAlphaDigit(digit); }
/** * Converts a digit into an lowercase hexadecimal character or the null character if invalid. * * @param digit a number 0 - 15 * @return the hex character for that digit or '\0' if invalid */ public static char getLowerCaseHex(final int digit) { if (digit < 0 || digit >= 16) { return '\0'; } return digit < 10 ? getNumericalDigit(digit) : getLowerCaseAlphaDigit(digit); }