Java Character forDigit() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

In Java, Character.forDigit(digit, radix) is the reverse of digit(): it takes a number and returns the character that represents it in the given radix. forDigit(5, 10) is '5' and forDigit(11, 16) is 'b'.

Values 0 to 9 become '0' to '9'; values 10 to 35 become the lowercase letters 'a' to 'z'. If the radix is outside 2 to 36, or the digit is negative or not smaller than the radix, the method returns the null character '\u0000' instead of throwing.

A static method of java.lang.Character (Java 1.0+): call it on the class, as in Character.forDigit(...); you do not need a Character object. No import is needed, because java.lang is always available.

Syntax

Pass the digit value and the radix; the result is a char.

Java
char a = Character.forDigit(5, 10);    // '5'
char b = Character.forDigit(11, 16);   // 'b' (always lowercase)
char c = Character.forDigit(35, 36);   // 'z'
char d = Character.forDigit(10, 10);   // '\u0000', 10 is not a decimal digit
CallWhat it does
Character.forDigit(n % 16, 16)The last hex digit of n.
Character.toUpperCase(Character.forDigit(d, 16))Uppercase hex digits.
Character.forDigit(d, r) == '\0'Detect invalid input.

There is only one overload, forDigit(int digit, int radix), available since Java 1.0. Integer.toString(n, radix) and Integer.toHexString(n) build whole numbers and also use lowercase letters.

Library declarations
Java
// java.lang.Character
public static char forDigit(int digit, int radix)

Parameters

ParameterDescription
digit (int)The value to convert, from 0 to radix - 1.
radix (int)The base, from Character.MIN_RADIX (2) to Character.MAX_RADIX (36).

Return Value

Return ValueDescription
'0' to '9', 'a' to 'z'The digit character.
'\u0000'The null character, for an invalid digit or radix.

Compare with Similar Methods

Related conversions:

MethodHow it differs
digit(ch, radix)The reverse: digit character to number, -1 when invalid.
Integer.toString(n, radix)Converts a whole int, including the sign.
(char) ('0' + d)Decimal only, no validation.

Java Examples

Example 1 — Write a Number in Base 2, 16, and 36

Repeatedly divide by the radix and turn each remainder into a digit character.

Java
public class CharacterForDigitBase {
    static String toBase(int n, int radix) {
        if (n == 0) return "0";
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            sb.append(Character.forDigit(n % radix, radix));
            n /= radix;
        }
        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        System.out.println("500 in base 2:  " + toBase(500, 2));
        System.out.println("500 in base 16: " + toBase(500, 16));
        System.out.println("500 in base 36: " + toBase(500, 36));
    }
}

How It Works

  1. Remainders are digits. n % radix is always between 0 and radix - 1, so forDigit always gets valid input here.
  2. Built backwards. The first remainder is the last digit, so the builder is reversed at the end; for 16 the remainders are 4, 15, and 1, which read as 1f4.
  3. Base 36. 500 = 13 * 36 + 32, and digits 13 and 32 are the letters d and w.

Example 2 — Valid Digits and the Null Character

Print the result for valid calls, and show what invalid calls return.

Java
public class CharacterForDigitInvalid {
    static String show(int digit, int radix) {
        char c = Character.forDigit(digit, radix);
        return "forDigit(" + digit + ", " + radix + ") = " + (c == '\0' ? "null char (code 0)" : String.valueOf(c));
    }

    public static void main(String[] args) {
        System.out.println(show(5, 10));
        System.out.println(show(11, 16));
        System.out.println(show(35, 36));
        System.out.println(show(10, 10));
        System.out.println(show(-1, 10));
        System.out.println(show(3, 40));
    }
}

How It Works

  1. Lowercase letters. 11 in base 16 is b, not B, and 35 in base 36 is z.
  2. Three ways to fail. 10 is not below the radix 10, -1 is negative, and 40 is above MAX_RADIX; each returns '\0'.
  3. Never print '\0' directly. The helper turns the null character into readable text, because printing it would put an invisible control character on the console.

Example 3 — Format Bytes as Two Uppercase Hex Digits

Split each byte into its high and low four bits and convert both with forDigit.

Java
public class CharacterForDigitHexBytes {
    static String hex2(int value) {
        char hi = Character.forDigit((value >> 4) & 0xF, 16);
        char lo = Character.forDigit(value & 0xF, 16);
        return "" + Character.toUpperCase(hi) + Character.toUpperCase(lo);
    }

    public static void main(String[] args) {
        byte[] data = {(byte) 0xAF, 0x07, 0x3C};
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(hex2(b)).append(' ');
        }
        System.out.println("bytes: " + sb.toString().trim());
    }
}

How It Works

  1. Two nibbles. (value >> 4) & 0xF is the high four bits and value & 0xF the low four; for 0xAF they are 10 and 15.
  2. Masks handle negative bytes. (byte) 0xAF is -81 as a signed byte, but masking with 0xF after the shift keeps only the bits that matter.
  3. Uppercase and padding. toUpperCase turns a and f into A and F, and 0x07 keeps its leading zero because both nibbles are always printed. Starting with "" makes + join the two chars as text instead of adding their codes.

Frequently Asked Questions

The null character '\u0000'. It does not throw an exception, so check for it if the input might be out of range.
Always lowercase: forDigit(10, 16) is 'a'. Use Character.toUpperCase on the result for uppercase.
They are opposites. forDigit turns a value into a character, digit turns a character into a value.
For 0 to 15, Character.forDigit(n, 16). For a whole number, Integer.toHexString(n).
Yes. Character.MAX_RADIX is 36, so forDigit(35, 36) returns 'z'.

Next: getDirectionality()

Get the bidirectional (left-to-right or right-to-left) class of a character.

getDirectionality() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful