diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java index b96d20191..87ac35aac 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java @@ -363,7 +363,7 @@ public class TCharacter extends TObject implements TComparable { } public static char forDigit(int digit, int radix) { - if (radix < MIN_RADIX || radix > MAX_RADIX || digit >= radix) { + if (radix < MIN_RADIX || radix > MAX_RADIX || digit < 0 || digit >= radix) { return '\0'; } return digit < 10 ? (char) ('0' + digit) : (char) ('a' + digit - 10); diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java index 63938fefa..82d8637e2 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java @@ -77,4 +77,15 @@ public class CharacterTest { assertEquals((char) 12405, Character.reverseBytes((char) 30000)); assertEquals((char) -12150, Character.reverseBytes((char) -30000)); } + + @Test + public void characterForDigit() { + assertEquals('\0', Character.forDigit(-1, 5)); + assertEquals('\0', Character.forDigit(6, 5)); + assertEquals('\0', Character.forDigit(1, 1)); + assertEquals('\0', Character.forDigit(1, 37)); + assertEquals('a', Character.forDigit(10, 11)); + assertEquals('\0', Character.forDigit(10, 10)); + assertEquals('5', Character.forDigit(5, 6)); + } }