implement Character.forDigit

This commit is contained in:
Joel Dice 2011-01-20 09:33:50 -07:00
parent 2dcc1d525a
commit b57e734ceb
2 changed files with 15 additions and 0 deletions

View File

@ -109,6 +109,15 @@ public final class Character implements Comparable<Character> {
} }
} }
public static char forDigit(int digit, int radix) {
if (MIN_RADIX <= radix && radix <= MAX_RADIX) {
if (0 <= digit && digit < radix) {
return (char) (digit < 10 ? digit + '0' : digit + 'a' - 10);
}
}
return 0;
}
public static boolean isLetter(int c) { public static boolean isLetter(int c) {
return canCastToChar(c) && isLetter((char) c); return canCastToChar(c) && isLetter((char) c);
} }

View File

@ -71,5 +71,11 @@ public class Strings {
sb.append('$'); sb.append('$');
sb.append('2'); sb.append('2');
expect(sb.substring(1).equals("2")); expect(sb.substring(1).equals("2"));
expect(Character.forDigit(Character.digit('0', 10), 10) == '0');
expect(Character.forDigit(Character.digit('9', 10), 10) == '9');
expect(Character.forDigit(Character.digit('b', 16), 16) == 'b');
expect(Character.forDigit(Character.digit('f', 16), 16) == 'f');
expect(Character.forDigit(Character.digit('z', 36), 36) == 'z');
} }
} }