[java] How do I convert a number to a letter in Java?

Is there a nicer way of converting a number to its alphabetic equivalent than this?

private String getCharForNumber(int i) {
    char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    if (i > 25) {
        return null;
    }
    return Character.toString(alphabet[i]);
}

Maybe something than can deal with numbers greater than 26 more elegantly too?

This question is related to java

The answer is


public static string IntToLetters(int value)
{
string result = string.Empty;
while (--value >= 0)
{
    result = (char)('A' + value % 26 ) + result;
    value /= 26;
}
return result;
}

To meet the requirement of A being 1 instead of 0, I've added -- to the while loop condition, and removed the value-- from the end of the loop, if anyone wants this to be 0 for their own purposes, you can reverse the changes, or simply add value++; at the beginning of the entire method.


You can convert the input to base 26 (Hexavigesimal) and convert each "digit" back to base 10 individually and apply the ASCII mapping. Since A is mapped to 0, you will get results A, B, C,..., Y, Z, BA, BB, BC,...etc, which may or may not be desirable depending on your requirements for input values > 26, since it may be natural to think AA comes after Z.

public static String getCharForNumber(int i){

    // return null for bad input
    if(i < 0){
        return null;
    }

    // convert to base 26
    String s = Integer.toString(i, 26);

    char[] characters = s.toCharArray();

    String result = "";
    for(char c : characters){
        // convert the base 26 character back to a base 10 integer
        int x = Integer.parseInt(Character.valueOf(c).toString(), 26);
        // append the ASCII value to the result
        result += String.valueOf((char)(x + 'A'));          
    }

    return result;
}

This isn't exactly an answer, but some useful/related code I made. When you run it and enter any character in the command line it returns getNumericValue(char) ... which doesn't seem to be the same as the ASCII table so be aware. Anyways, not a direct answer to your question but hopefully helpful:

import java.lang.*;
import java.util.*;
/* charVal.java
         */

//infinite loops. whenever you type in a character gives you value of 
//getNumericValue(char)
//
//ctrl+c to exit



public class charVal {
   public static void main(String[] args) {
     Scanner inPut = new Scanner(System.in);
     for(;;){
       char c = inPut.next().charAt(0);
       System.out.printf("\n %s = %d \n", c, Character.getNumericValue(c));
     }
    }
}

Personally, I prefer

return "ABCDEFGHIJKLMNOPQRSTUVWXYZ".substring(i, i+1);

which shares the backing char[]. Alternately, I think the next-most-readable approach is

return Character.toString((char) (i + 'A'));

which doesn't depend on remembering ASCII tables. It doesn't do validation, but if you want to, I'd prefer to write

char c = (char) (i + 'A');
return Character.isUpperCase(c) ? Character.toString(c) : null;

just to make it obvious that you're checking that it's an alphabetic character.


I would return a character char instead of a string.

public static char getChar(int i) {
    return i<0 || i>25 ? '?' : (char)('A' + i);
}

Note: when the character decoder doesn't recognise a character it returns ?

I would use 'A' or 'a' instead of looking up ASCII codes.


Rather than giving an error or some sentinel value (e.g. '?') for inputs outside of 0-25, I sometimes find it useful to have a well-defined string for all integers. I like to use the following:

   0 ->    A
   1 ->    B
   2 ->    C
 ...
  25 ->    Z
  26 ->   AA
  27 ->   AB
  28 ->   AC
 ...
 701 ->   ZZ
 702 ->  AAA
 ...

This can be extended to negatives as well:

  -1 ->   -A
  -2 ->   -B
  -3 ->   -C
 ...
 -26 ->   -Z
 -27 ->  -AA
 ...

Java Code:

public static String toAlphabetic(int i) {
    if( i<0 ) {
        return "-"+toAlphabetic(-i-1);
    }

    int quot = i/26;
    int rem = i%26;
    char letter = (char)((int)'A' + rem);
    if( quot == 0 ) {
        return ""+letter;
    } else {
        return toAlphabetic(quot-1) + letter;
    }
}

Python code, including the ability to use alphanumeric (base 36) or case-sensitive (base 62) alphabets:

def to_alphabetic(i,base=26):
    if base < 0 or 62 < base:
        raise ValueError("Invalid base")

    if i < 0:
        return '-'+to_alphabetic(-i-1)

    quot = int(i)/base
    rem = i%base
    if rem < 26:
        letter = chr( ord("A") + rem)
    elif rem < 36:
        letter = str( rem-26)
    else:
        letter = chr( ord("a") + rem - 36)
    if quot == 0:
        return letter
    else:
        return to_alphabetic(quot-1,base) + letter

public static String abcBase36(int i) {
    char[] ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    int quot = i / 36;
    int rem = i % 36;

    char letter = ALPHABET[rem];
    if (quot == 0) {
        return "" + letter;
    } else {
        return abcBase36(quot - 1) + letter;
    }
}

public static void main(String[] args)
 {
     int rem,n=702,quo;
     String s=" ";
     while(n>0)
     {
         rem=(n-1)%26;
         quo=(n-1)/26;
         s=(char)(rem+97)+s;
         if(quo==1)
         {
            s=(char)(97)+s;
            break;
         }
         else
         n=(n-1)/26;
     }
     System.out.print(s);
 }
}
////We can also write the code like the below one. There is no much difference but it may help to understand the concept for some people.


public static void main(String[] args)
 {
     int rem,n=52,quo;
     String s=" ";
     while(n>0)
     {
         rem=n%26;
         quo=n/26;
         if(rem==0)
             rem=26;
         s=(char)(rem+96)+s;
         if((quo==1 || quo==0) && n>26)
         {
             n=n/26;
            s=(char)(n+96)+s;
            break;
         }
         else
             n=n/26-1; 
     }
     System.out.print(s);
 }

for(int i=0;i<ar.length();i++) {
            char ch = ar.charAt(i);
                    System.out.println((char)(ch+16));;
        }

Another approach starting from 0 and returning a String

public static String getCharForNumber(int i) {
    return i < 0 || i > 25 ? "?" : String.valueOf((char) ('A' + i));
}

if you define a/A as 0

char res;
if (i>25 || i<0){
    res = null;
}
    res = (i) + 65
}
return res;

65 for captitals; 97 for non captitals


You can try like this:

private String getCharForNumber(int i) {
    CharSequence css = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (i > 25) {
        return null;
    }
    return css.charAt(i) + "";
}

Another variant:

private String getCharForNumber(int i) {
    if (i > 25 || i < 0) {
        return null;
    }
    return new Character((char) (i + 65)).toString();
}

Getting the alphabetical value from an int can be simply done with:

(char)('@' + i)