diff --git a/classpath/java/lang/Character.java b/classpath/java/lang/Character.java index f696037561..88025280e8 100644 --- a/classpath/java/lang/Character.java +++ b/classpath/java/lang/Character.java @@ -109,6 +109,15 @@ public final class Character implements Comparable { } } + 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) { return canCastToChar(c) && isLetter((char) c); } diff --git a/test/Strings.java b/test/Strings.java index 6b993bcb4f..d98c1f13f9 100644 --- a/test/Strings.java +++ b/test/Strings.java @@ -71,5 +71,11 @@ public class Strings { sb.append('$'); sb.append('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'); } }