Java Character digit() Method

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

Definition

In Java, Character.digit(ch, radix) returns the value of the character as a digit in the given number base, called the radix. digit('7', 10) is 7, digit('f', 16) is 15, and digit('z', 36) is 35.

If the character is not a digit in that radix, or the radix is outside Character.MIN_RADIX (2) to Character.MAX_RADIX (36), it returns -1. Latin letters a to z (in either case) stand for 10 to 35, and every Unicode decimal digit, such as Arabic-Indic three, is accepted too.

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

Unicode version.

The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.

Syntax

Pass the character and the radix.

Java
int a = Character.digit('7', 10);    // 7
int b = Character.digit('F', 16);    // 15 (case does not matter)
int c = Character.digit('9', 8);     // -1, 9 is not an octal digit
int d = Character.digit('z', 36);    // 35
CallWhat it does
Character.digit(c, 10)Decimal digit value, or -1.
Character.digit(c, 16)Hex digit value 0 to 15, or -1.
Character.digit(c, 2) != -1Is this a binary digit (0 or 1)?

Overloads: digit(char ch, int radix) and digit(int codePoint, int radix). Integer.parseInt(s, radix) uses this same per-character rule to read whole numbers.

Library declarations
Java
// java.lang.Character
public static int digit(char ch, int radix)
public static int digit(int codePoint, int radix)   // since Java 5

Parameters

ParameterDescription
ch / codePointThe character to convert.
radix (int)The number base, from Character.MIN_RADIX (2) to Character.MAX_RADIX (36).

Return Value

Return ValueDescription
0 to radix - 1The digit's value in that radix.
-1Not a digit in that radix, or the radix is out of range.

Compare with Similar Methods

Other ways to turn a character into a number:

MethodHow it differs
c - '0'Fast, but correct only for ASCII '0' to '9', with no error checking.
getNumericValue(ch)No radix; also gives values for Roman numerals and superscripts.
forDigit(d, radix)The reverse: number to digit character.

Java Examples

Example 1 — Convert a Hex String to an int by Hand

Read "1F4" digit by digit, as Integer.parseInt(s, 16) does internally.

Java
public class CharacterDigitHex {
    public static void main(String[] args) {
        String hex = "1F4";
        int value = 0;
        for (int i = 0; i < hex.length(); i++) {
            int d = Character.digit(hex.charAt(i), 16);
            if (d == -1) {
                System.out.println("bad digit at " + i);
                return;
            }
            value = value * 16 + d;
        }
        System.out.println(hex + " in base 16 = " + value);
    }
}

How It Works

  1. Letter digits. digit('F', 16) is 15; uppercase and lowercase hex letters give the same value.
  2. Positional value. Each step multiplies the total by 16 and adds the next digit: 1, then 1 * 16 + 15 = 31, then 31 * 16 + 4 = 500.
  3. Fail early on -1. A character such as 'G' would give -1, and the program reports its position instead of building a wrong number.

Example 2 — Valid and Invalid Digits in Different Radixes

The same characters give different answers depending on the radix, and an impossible radix always gives -1.

Java
public class CharacterDigitRadix {
    public static void main(String[] args) {
        System.out.println("digit('7', 10) = " + Character.digit('7', 10));
        System.out.println("digit('7', 8)  = " + Character.digit('7', 8));
        System.out.println("digit('9', 8)  = " + Character.digit('9', 8));
        System.out.println("digit('f', 16) = " + Character.digit('f', 16));
        System.out.println("digit('g', 16) = " + Character.digit('g', 16));
        System.out.println("digit('z', 36) = " + Character.digit('z', 36));
        System.out.println("digit('1', 1)  = " + Character.digit('1', 1));
        System.out.println("digit('1', 37) = " + Character.digit('1', 37));
    }
}

How It Works

  1. A digit must be smaller than the radix. 7 is valid in octal, but 9 is not, because octal digits stop at 7.
  2. Letters continue after 9. In base 16, f is 15 and g (16) is too big; in base 36, z reaches the maximum value 35.
  3. Radix limits. 1 and 37 are outside MIN_RADIX to MAX_RADIX, so the method returns -1 instead of throwing.

Example 3 — digit() vs c - '0' on Non-ASCII Digits

Read an Arabic-Indic number correctly, and see how the subtraction shortcut goes wrong.

Java
public class CharacterDigitUnicode {
    public static void main(String[] args) {
        String arabic = "\u0661\u0662\u0663";
        int value = 0;
        for (char c : arabic.toCharArray()) {
            value = value * 10 + Character.digit(c, 10);
        }
        System.out.println("parsed with digit(): " + value);
        System.out.println("'\\u0663' - '0' = " + ('\u0663' - '0'));
        System.out.println("digit of superscript two: " + Character.digit('\u00B2', 10));
    }
}

How It Works

  1. Real decimal digits. U+0661, U+0662, and U+0663 are ARABIC-INDIC DIGIT ONE, TWO, and THREE, so digit(c, 10) returns 1, 2, and 3 and the loop builds 123.
  2. The shortcut breaks. '\u0663' - '0' subtracts the code 48 from 1635 and prints 1587; that only works for ASCII digits.
  3. Look-alikes still fail. Superscript two is not a decimal digit, so digit returns -1. The label uses "\\u0663" with a doubled backslash, so it prints as text rather than becoming the digit itself.

Frequently Asked Questions

Character.digit(c, 10) returns 0 to 9, or -1 if c is not a digit. c - '0' also works, but only for ASCII digits and without any check.
-1. It also returns -1 when the radix is below 2 or above 36; it never throws an exception.
No. digit('f', 16) and digit('F', 16) both return 15.
Character.MAX_RADIX, which is 36: the digits 0 to 9 plus the letters a to z.
digit takes a radix and only accepts digits valid in it. getNumericValue has no radix and also returns values for characters such as Roman numerals.

Next: equals()

Check whether an object is a Character holding the same char value.

equals() 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.

15 people found this page helpful