简体   繁体   中英

How do I convert a number to a letter in Java?

有没有比这更好的将数字转换为其对应字母的方法?

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

Just make use of the ASCII representation.

private String getCharForNumber(int i) {
    return i > 0 && i < 27 ? String.valueOf((char)(i + 64)) : null;
}

Note: This assumes that i is between 1 and 26 inclusive.

You'll have to change the condition to i > -1 && i < 26 and the increment to 65 if you want i to be zero-based.

Here is the full ASCII table, in case you need to refer to:


在此处输入图片说明


Edit:

As some folks suggested here, it's much more readable to directly use the character 'A' instead of its ASCII code.

private String getCharForNumber(int i) {
    return i > 0 && i < 27 ? String.valueOf((char)(i + 'A' - 1)) : null;
}

Rather than giving an error or some sentinel value (eg '?') 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

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

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.

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;
    }
}

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;
}
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.

从 int 获取字母值可以简单地完成:

(char)('@' + i)

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();
}

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));
     }
    }
}

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));
}
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));;
        }

The accepted answer best describes the solution. Nevertheless, it does not handle the case when you want to keep numbering over the value 27 like aa , ab , ... az , aaa , aab .... For doing so I achieved with the following:

protected String getAlphaNumber(int intValue) {
    if (intValue == 0) {
        return "";
    }
    intValue = Math.abs(intValue);
    String prefix = "";
    int charPosition = intValue % 26;
    int blocks = intValue / 26;
    if (charPosition == 0) { //the last letter, "z" (26)
        blocks--;
        charPosition = 26;
    }
    for (int i = 0; i < blocks; i++) {
        prefix += "a";
    }
    return prefix + ((char) (charPosition + 96)); // "a"-"z" chars are located in the 97-122 indexes of the ASCII table, so shift all 96 positions.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM