if (i >= 0 && i <= 9) { char c = Character.forDigit(i, 10); .... }
/** * Formats an {@code int} as a 4-bit unsigned hex nibble. * * @param v value to format * @return {@code non-null;} formatted form */ public static String uNibble(int v) { char[] result = new char[1]; result[0] = Character.forDigit(v & 0x0f, 16); return new String(result); }
/** * Formats a {@code long} as an 8-byte unsigned hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String u8(long v) { char[] result = new char[16]; for (int i = 0; i < 16; i++) { result[15 - i] = Character.forDigit((int) v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Formats an {@code int} as a 4-byte unsigned hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String u4(int v) { char[] result = new char[8]; for (int i = 0; i < 8; i++) { result[7 - i] = Character.forDigit(v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Formats an {@code int} as a 3-byte unsigned hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String u3(int v) { char[] result = new char[6]; for (int i = 0; i < 6; i++) { result[5 - i] = Character.forDigit(v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Formats an {@code int} as a 2-byte unsigned hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String u2(int v) { char[] result = new char[4]; for (int i = 0; i < 4; i++) { result[3 - i] = Character.forDigit(v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Formats an {@code int} as a 1-byte unsigned hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String u1(int v) { char[] result = new char[2]; for (int i = 0; i < 2; i++) { result[1 - i] = Character.forDigit(v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Returns the upper case hex digit of the lower 4 bits of the int. * * @param b the input int * @return the upper case hex digit of the lower 4 bits of the int. */ static char hexDigit(final int b) { return Character.toUpperCase(Character.forDigit(b & 0xF, RADIX)); }
/** * Formats an {@code int} as a 2-byte signed hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String s2(int v) { char[] result = new char[5]; if (v < 0) { result[0] = '-'; v = -v; } else { result[0] = '+'; } for (int i = 0; i < 4; i++) { result[4 - i] = Character.forDigit(v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Formats an {@code int} as a 4-byte signed hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String s4(int v) { char[] result = new char[9]; if (v < 0) { result[0] = '-'; v = -v; } else { result[0] = '+'; } for (int i = 0; i < 8; i++) { result[8 - i] = Character.forDigit(v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Formats an {@code int} as a 1-byte signed hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String s1(int v) { char[] result = new char[3]; if (v < 0) { result[0] = '-'; v = -v; } else { result[0] = '+'; } for (int i = 0; i < 2; i++) { result[2 - i] = Character.forDigit(v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * Formats a {@code long} as an 8-byte signed hex value. * * @param v value to format * @return {@code non-null;} formatted form */ public static String s8(long v) { char[] result = new char[17]; if (v < 0) { result[0] = '-'; v = -v; } else { result[0] = '+'; } for (int i = 0; i < 16; i++) { result[16 - i] = Character.forDigit((int) v & 0x0f, 16); v >>= 4; } return new String(result); }
/** * This returns a string from decimal digit smallestDigit to decimal digit * biggest digit. Smallest digit is labeled 1, and the limits are * inclusive. */ public static String truncate(int n, int smallestDigit, int biggestDigit) { int numDigits = biggestDigit - smallestDigit + 1; char[] result = new char[numDigits]; for (int j = 1; j < smallestDigit; j++) { n = n / 10; } for (int j = numDigits - 1; j >= 0; j--) { result[j] = Character.forDigit(n % 10, 10); n = n / 10; } return new String(result); }
/** * Converts the number to a String in a given radix. * This uses {@link Character.digit} to convert each digit * to one character. * @param radix the radix to use, between {@link Character.MIN_RADIX} * and {@link Character.MAX_RADIX}. * @return a String containing the digits of this number in the * specified radix, using '0' .. '9' and 'a' .. 'z' (as much as needed). */ public String toString(int radix) { if(radix < Character.MIN_RADIX || Character.MAX_RADIX < radix) { throw new IllegalArgumentException("radix out of range: " + radix); } if(digits.length == 0) return "0"; int[] rdigits = convertTo(radix); StringBuilder b = new StringBuilder(rdigits.length); for(int dig : rdigits) { b.append(Character.forDigit(dig, radix)); } return b.toString(); }
/** * @param len Length. * @return Test string. */ private static String generateTestString(int len) { assert len > 0; SB sb = new SB(); for (int i = 0; i < len; i++) sb.a(Character.forDigit(i % 10, 10)); return sb.toString(); }