java 将字母转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4262567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:35:20  来源:igfitidea点击:

Convert letter to digits

java

提问by Leostrada

I want to change the letters A to point 1 and so the letter Z to be number 26, then changed again to number 27 letters AA, AB to 28. How do I? Do I have to use the "switch"? I use java program.

我想将字母 A 更改为点 1,因此将字母 Z 更改为数字 26,然后再次更改为数字 27 的字母 AA,AB 为 28。我该怎么做?我必须使用“开关”吗?我使用java程序。

回答by icyrock.com

Did not test this, but something along these lines should work:

没有对此进行测试,但是应该按照以下方式进行操作:

public String numberToCharacterRepresentation(int number) {
  char[] ls = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
  String r = "";
  while(true) {
    r = ls[number % 26] + r;
    if(number < 26) {
      break;
    }
    number /= 26;
  }
  return r;
}

The reverse:

相反:

public int stringToNumber(String str) {
  char[] ls = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
  Map<Character, Integer> m = new HashMap<Character, Integer>();
  int j = 0;
  for(char c: ls) {
    m.put(c, j++);
  }
  int i = 0;
  int mul = 1;
  for(char c: new StringBuffer(str).reverse().toString().toCharArray()) {
    i += m.get(c) * mul;
    mul *= ls.length;
  }
  return i;
}

回答by sanssucre

Use the Character object 0=>0 a=>10, etc If you only use letters then subtract 10

使用 Character 对象 0=>0 a=>10 等等 如果你只使用字母然后减去 10

Character.forDigit(10,Character.MAX_RADIX) //will return 'a'
Character.getNumericValue('a') // will return 10

回答by Peter Lawrey

A simple solution is to treat the problem like writing letters instead of digits.

一个简单的解决方案是将问题视为写字母而不是数字。

public static String asLetters(long num) {
   StringBuilder sb = new StringBuilder();
   while(num > 0) {
      sb.append((char) ('@' + num % 26));
      num /= 26;
   }
   return sb.toString();
}

回答by loeschg

For those of you wanting to do this for Excel:

对于那些想要为 Excel 执行此操作的人:

public String getEquivColumn(int number){
    String converted = "";
    // Repeatedly divide the number by 26 and convert the
    // remainder into the appropriate letter.
    while (number >= 0)
    {
        int remainder = number % 26;
        converted = (char)(remainder + 'A') + converted;
        number = (number / 26) - 1;
    }

    return converted;
}

回答by Arif

This will work for A to ZZ :

这适用于 A 到 ZZ :

public static int columnCharToNumber(String str) {
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if(str.length() == 1) {
        return alphabet.indexOf(str);
    }
    if(str.length() == 2) {
        return ( alphabet.indexOf(str.substring(1)) + 26*(1+alphabet.indexOf(str.substring(0,1)))) ;
    }
    return -1;
}

回答by user519627

import javax.swing.JOptionPane;

public class TBesar{

    public static long x(int a, int b){
        if (b==0){
            return(1);
        }
        else{
            return(a*(x(a,(b-1))));
        }
    }
    public static long KatakeAngka(String nama){
        int A = 0;
        int B = 26;
        long C = 0;
        long Z;
        int panjang = nama.length();
        char namas[] = new char[panjang];
        for (int i=0;i<panjang;i++){
        namas[i] = nama.charAt(i);
            switch (namas[i]){  
                case 'a' : A=1;break;
                case 'b' : A=2;break;
                case 'c' : A=3;break;
                case 'd' : A=4;break;
                case 'e' : A=5;break;
                case 'f' : A=6;break;
                case 'g' : A=7;break;
                case 'h' : A=8;break;
                case 'i' : A=9;break;
                case 'j' : A=10;break;
                case 'k' : A=11;break;
                case 'l' : A=12;break;
                case 'm' : A=13;break;
                case 'n' : A=14;break;
                case 'o' : A=15;break;
                case 'p' : A=16;break;
                case 'q' : A=17;break;
                case 'r' : A=18;break;
                case 's' : A=19;break;
                case 't' : A=20;break;
                case 'u' : A=21;break;
                case 'v' : A=22;break;
                case 'x' : A=23;break;
                case 'w' : A=24;break;
                case 'y' : A=25;break;
                case 'z' : A=26;break;
            }
            int D = panjang-(i+1);
            Z = (x(B,D))*A;
            C = C+Z;
        }return(C);
    }
    public static String hitung(long angka){
        String B ;
        if(angka<27){
            if(angka==1){
                B="a";
            }else if(angka==2){
                B="b";
            }else if(angka==3){
                B="c";
            }else if(angka==4){
                B="d";
            }else if(angka==5){
                B="e";
            }else if(angka==6){
                B="f";
            }else if(angka==7){
                B="g";
            }else if(angka==8){
                B="h";
            }else if(angka==9){
                B="i";
            }else if(angka==10){
                B="j";
            }else if(angka==11){
                B="k";
            }else if(angka==12){
                B="l";
            }else if(angka==13){
                B="m";
            }else if(angka==14){
                B="n";
            }else if(angka==15){
                B="o";
            }else if(angka==16){
                B="p";
            }else if(angka==17){
                B="q";
            }else if(angka==18){
                B="r";
            }else if(angka==19){
                B="s";
            }else if(angka==20){
                B="t";
            }else if(angka==21){
                B="u";
            }else if(angka==22){
                B="v";
            }else if(angka==23){
                B="w";
            }else if(angka==24){
                B="x";
            }else if(angka==25){
                B="y";
            }else{B="z";}
            return(B);
        }
        else{
            return(hitung(angka/26)+hitung(angka%26));
        }
    }
    public static void main (String [] args){
        String kata = JOptionPane.showInputDialog(null,"Masukkan Kata ke 1");
        String kata2 = JOptionPane.showInputDialog(null, "Masukkan Kata ke 2");
        long hasil = KatakeAngka(kata);
        long hasil2 = KatakeAngka(kata2);
        long total = hasil+hasil2;

        String HasilKata = hitung(total);
        JOptionPane.showMessageDialog(null,kata+" = "+hasil+"\n"+kata2+" = "+hasil2+"\n"+kata+" + "+kata2+" = "+HasilKata);
    }
}

回答by JOTN

How about using c-'A'+1 to convert the letter in c to the number you want? Calculating the next place would be the same except add 27 instead. Basically what you're doing is converting a base-26 number to decimal except you have no zero.

使用 c-'A'+1 将 c 中的字母转换为您想要的数字如何?计算下一个位置将是相同的,除了添加 27。基本上你正在做的是将 base-26 数字转换为十进制,除非你没有零。

回答by Stephen C

This will do the job.

这将完成工作。

public String map(int i) {
    String res = "";
    if (i <= 0) {
        throw new IllegalArgumentException("Can only map +ve numbers");
    }
    while (i > 0) {
        res = Character.toString('A' + ((i - 1) % 26)) + res;
        i = i / 26;
    }
    return res;
}

A more complicated version using a StringBuilderwould be a more efficient, but this one is easier to understand.

使用 a 的更复杂版本StringBuilder会更有效,但这个更容易理解。

回答by GreenMatt

Perhaps the simplest way for A-Z would be something like:

也许 AZ 最简单的方法是这样的:

char c = *whatever letter you need*;
int cAsInt = Integer.toString(c - '@');  // @ is 1 less than A

For things like AA, BB, etc., it would depend on how many combinations you need. Setting up a mapping might be quickest, but if the possibilities are endless, you'll have to figure out some formula.

对于 AA、BB 等,这取决于您需要多少组合。设置映射可能是最快的,但如果可能性无穷无尽,您就必须找出一些公式。