Number to Letter

Number to Letter › Number to letter Java

Number to letter Java

Letters
Java code for these numbers

 
char arithmeticUTF-16 code units

In Java, convert a number from 1 to 26 to a letter with (char) ('A' + n - 1). A char is a 16-bit number, so 'A' (65) plus 2 is 67, and the cast turns 67 into 'C'. Use 'a' instead of 'A' for lowercase. For spreadsheet columns, where 28 is "AB," build the string with (n - 1) % 26 in a loop.

Type numbers into the tool to get the letters and a complete Java class you can run with java NumToLetter.java. The char arithmetic panel shows each step: the code of 'A', the offset, the resulting int and its bits, and the char it casts to.

How number to letter Java calculator works?

To convert a number to a letter in Java, the number to letter Java calculator adds 64 to the number and reads the character with that code. The calculator splits your list of numbers, checks each one, converts 1–26 with (char) ('A' + n − 1), or builds column letters (27 = AA) in column mode, and writes runnable Java code for the same numbers. 8 5 12 12 15 becomes HELLO.

  1. Read the numbers

    Spaces, commas, or new lines separate the numbers.

    8 5 12 12 15
  2. Pick the mode

    A1Z26 maps 1–26 to A–Z. Column mode maps any positive number to Excel column letters.

    A1Z26
  3. Run (char) ('A' + n − 1)

    Each number n becomes the character with code 64 + n (A = 65 in ASCII and Unicode).

    (char) ('A' + 7) = 'H'
  4. Write the code

    The Java snippet and its output appear side by side. Tick lowercase to use code 96 + n.

    HELLO

Column mode inserts (char) ('A' + r) at the front of a StringBuilder while n > 0, with r = (n − 1) % 26 and n = (n − 1) / 26. The (char) cast matters: without the cast, Java prints the int 72 instead of H.

Number to letter Java formula

The number to letter Java formula is a code point offset. letter = character(64 + n) for capitals and character(96 + n) for lowercase, for n from 1 to 26. Column letters use bijective base 26: take (n − 1) mod 26, then divide (n − 1) by 26, and repeat.

Formula

letter = (char) ('A' + n − 1)
lower = character(96 + n)
col(n): r = (n − 1) mod 26, n = ⌊(n − 1) ÷ 26⌋, repeat

Where

n
number from 1 to 26 (A1Z26) or any positive whole number (columns)
64
ASCII code of @, the character before A (65)
96
ASCII code of `, the character before a (97)
r
remainder 0–25, read as A–Z

Worked example

n = 28, column mode(28 − 1) % 26 = 1 → 'B'(28 − 1) / 26 = 1 → (1 − 1) % 26 = 0 → 'A'toColumn(28) = "AB"

Number to letter Java conversion chart

The number to letter Java conversion chart lists all 26 A1Z26 numbers with the ASCII codes and the Java expression. 1 = A (code 65), 13 = M (code 77), and 26 = Z (code 90).

nLetterCode (capital)Code (lowercase)Java expression
1A6597(char) ('A' + 0) → 'A'
2B6698(char) ('A' + 1) → 'B'
3C6799(char) ('A' + 2) → 'C'
4D68100(char) ('A' + 3) → 'D'
5E69101(char) ('A' + 4) → 'E'
6F70102(char) ('A' + 5) → 'F'
7G71103(char) ('A' + 6) → 'G'
8H72104(char) ('A' + 7) → 'H'
9I73105(char) ('A' + 8) → 'I'
10J74106(char) ('A' + 9) → 'J'
11K75107(char) ('A' + 10) → 'K'
12L76108(char) ('A' + 11) → 'L'
13M77109(char) ('A' + 12) → 'M'
14N78110(char) ('A' + 13) → 'N'
15O79111(char) ('A' + 14) → 'O'
16P80112(char) ('A' + 15) → 'P'
17Q81113(char) ('A' + 16) → 'Q'
18R82114(char) ('A' + 17) → 'R'
19S83115(char) ('A' + 18) → 'S'
20T84116(char) ('A' + 19) → 'T'
21U85117(char) ('A' + 20) → 'U'
22V86118(char) ('A' + 21) → 'V'
23W87119(char) ('A' + 22) → 'W'
24X88120(char) ('A' + 23) → 'X'
25Y89121(char) ('A' + 24) → 'Y'
26Z90122(char) ('A' + 25) → 'Z'

Column numbers past 26

nColumn letters
27AA
28AB
52AZ
53BA
78BZ
702ZZ
703AAA
1,000ALL
16,384XFD

Java number to letter examples

char c = (char) ('A' + 5 - 1);           // 'E'
String s = String.valueOf((char) (64 + 5)); // "E"
char lower = (char) ('a' + 5 - 1);       // 'e'

// letter to number
int n = Character.toUpperCase('e') - 'A' + 1; // 5

// whole message
int[] nums = {8, 5, 12, 12, 15};
StringBuilder sb = new StringBuilder();
for (int x : nums) sb.append((char) ('A' + x - 1));
System.out.println(sb); // HELLO

Without the cast, 'A' + 4 is an int (69), and printing it shows "69," not "E." Java promotes char to int in arithmetic, so cast the result back to char.

Column letters in Java

static String toColumn(int n) {
    StringBuilder sb = new StringBuilder();
    while (n > 0) {
        int r = (n - 1) % 26;
        sb.insert(0, (char) ('A' + r));
        n = (n - 1) / 26;
    }
    return sb.toString();
}
// toColumn(28) = "AB", toColumn(16384) = "XFD"

Apache POI has the same helper: CellReference.convertNumToColString(27) returns "AB," because POI counts columns from 0.

Frequently asked questions

How do I convert an int to a letter in Java?

Use (char) ('A' + n - 1) for n from 1 to 26, or (char) ('A' + n) if n starts at 0.

Why does Java print a number instead of a letter?

'A' + n is an int. Cast it with (char) before printing.

How do I convert a char to its alphabet position in Java?

Use Character.toUpperCase(c) - 'A' + 1. 'C' returns 3.

How do I get an Excel column letter in Java?

Use the loop on this page or Apache POI CellReference.convertNumToColString(index), which is 0-based.

What is the Java formula for number to letter?

The Java formula is (char) ('A' + n − 1), so n = 3 gives 'C'. Use 'a' in place of 'A' for lowercase.

What is (char) 65 in Java?

(char) 65 is 'A'. (char) 90 is 'Z' and (char) 97 is 'a'.

How do I convert an int array to letters in Java?

Loop over the array and append (char) ('A' + x − 1) to a StringBuilder. {3, 1, 20} builds "CAT".