从Java中的base10值转换为base60

时间:2018-11-13 09:14:53

标签: java radix

http://convertxy.com/index.php/numberbases/

在这里,我们可以看到许多基础calculations

https://stackoverflow.com/a/52474238/10309977

在这里,他们只建议使用base36。大写字母+ numbers

我想要的是将long值(以10为底)转换为大写+小写字母+数字。

因此,根据第一个网站,我可以使用这种方法。

我想从60转换为10,然后递增。

我该怎么做?

因为radix可以用于最大基数36。

1 个答案:

答案 0 :(得分:0)

这是完整的代码,假设A = 10且a = 36:

public static int base = 60;

public static void main(String[] args) {
    long test =0x7FFFFFFFFFFFFFFFL;
    System.out.println(test+ " -> "+convertBase10toCustomBase(test));

    String test2 = "FFDYWVtKFU7";
    System.out.println(test2+" -> "+convertCustomBasetoBase10(test2));

}
public static String convertBase10toCustomBase(long n){
    boolean negative = (n<0); // Remembers if n is negative.
    if (negative) n=-n; // Now works with a positive number
    String result= "";
    do {
        result= getBase((int)(n%base))+result;
        n/=base;
    }while(n>0);

    if(negative) result="-"+result;

    return result;
}
public static char getBase(int n){
    if(n<=9)
        return (char)(n+'0'); // Returns the char from numeric value
    if(n <35)
        return (char)('A'+ n -10); // Returns char in UpperCase if 9 < n < n+base 
    return (char)('a'+ n -36); // Returns in LowerCase otherwise
}

public static long convertCustomBasetoBase10(String s){

    boolean isNegative=false; // Remembers if the String starts with "-"
    if(s.charAt(0)=='-'){
        isNegative = true;
        s=s.substring(1); // Removes the "-" if negative
    }

    long result = 0;

    // Each char from right to left will be converted to a numeric value
    // Each numeric value must be multiplied by a certain power depending on its position, then added to previous ones.
    for (int i =0; i<s.length();i++)
        result += Math.pow(base,i)*getBase10(s.charAt(s.length()-i-1)); 

    if(isNegative) return -result;

    return result;

}
public static int getBase10(char c){
    // If the char represents a number, just return its value
    if (!Character.isLetter(c))
        return Character.getNumericValue(c);

    // If the char is UpperCase, we substract from it 
    // the value of 'A' and add 10 so that the first char A = 10
    if(Character.isUpperCase(c))
        return c -'A'+10;
    // If LowerCase, we substract  the value of 'a' and add 10+26
    return c-'a'+36;
}

显示:

  

9223372036854775807-> FFDYWVtKFU7

     

FFDYWVtKFU7-> 9223372036854775807

编辑::感谢Peter Lawrey建议直接使用char值而不是使用char数组进行计算。