在JAVA中,从基X到基Y的转换返回的字符与PHP中的相同函数不同

在JAVA中,从基X到基Y的转换返回的字符与PHP中的相同函数不同,java,string,character-encoding,byte,base,Java,String,Character Encoding,Byte,Base,我用JAVA创建了一个方法,以便对现有的PHP函数执行相同的操作,即:将任意大的数字从任意基转换为任意基 java方法工作正常,我可以将数字从一个基转换为另一个基,然后再转换回来,但结果字符串与PHP函数不同。这对我来说是个问题,因为我想在PHP中转换一个数字,然后再在JAVA中转换回来 例如,让我们转换数字998765;43210;9999;2来自Base11,字母表0123456789;要使用PHP和JAVA中的字母0123456789ABCDEFGHIJK访问Base21: PHP中的示例

我用JAVA创建了一个方法,以便对现有的PHP函数执行相同的操作,即:将任意大的数字从任意基转换为任意基

java方法工作正常,我可以将数字从一个基转换为另一个基,然后再转换回来,但结果字符串与PHP函数不同。这对我来说是个问题,因为我想在PHP中转换一个数字,然后再在JAVA中转换回来

例如,让我们转换数字998765;43210;9999;2来自Base11,字母表0123456789;要使用PHP和JAVA中的字母0123456789ABCDEFGHIJK访问Base21:

PHP中的示例结果:

convBase("998765;43210;9999;2", "0123456789;", "0123456789ABCDEFGHIJK") = "GJK7K6B2KKGKK96"
JAVA中的示例结果:

convBase("998765;43210;9999;2", "0123456789;", "0123456789ABCDEFGHIJK") = "1B0EJAJ0IG3DABI"
我希望结果是一样的,这样我就可以在PHP中转换一个数字,然后在JAVA中转换回来

我认为问题可能是字符编码,但我不知道如何解决它

PHP函数和测试:

JAVA方法和测试:


您的测试用例中有一个输入错误。这两个程序似乎都是正确的,或者至少是一致的

您的Java变体转换为98765;43210;9999;2当您的PHP程序转换998765时;43210;9999;2.注意开头的两个9。当我更改号码时,我得到以下输出:

Number = 998765;43210;9999;2
Converted = GJK7K6B2KKGKK96
Back = 998765;43210;9999;2

这与PHP版本的输出一致。

首先,我将使用一个小得多的数字进行测试;找出哪个结果是正确的,这样你就知道看哪个了。还要注意的是,Java不是首字母缩略词,也不应该是全大写的。您不需要对长度变量使用BigInteger,因为您不能让String.length大于Integer.MAX_值,这会更具可读性;与新的BigInteger 998765A43210A9999A2相同,11.ToString 21为什么要使用不同于所有人使用的字符集?所以我想说你的测试有问题;43210;9999;2.这只是你考试中的一个输入错误……你是对的。我感到羞愧!抱歉占用你们的时间。
import java.math.BigInteger;

public class ConvBase{

    public static String convBase(String number, String fromBaseInput, String toBaseInput){

        if (fromBaseInput.equals(toBaseInput))
            return number;

        BigInteger fromLen = new BigInteger(""+fromBaseInput.length());
        BigInteger toLen = new BigInteger(""+toBaseInput.length());
        BigInteger numberLen = new BigInteger(""+number.length());

        if(toBaseInput.equals("0123456789")){
            BigInteger retval = BigInteger.ZERO;
            for(int i=1; i<=number.length(); i++){
                retval = retval.add(
                    new BigInteger(""+fromBaseInput.indexOf(number.charAt(i-1))).multiply(
                        fromLen.pow(numberLen.subtract(new BigInteger(""+i)).intValue())
                        //pow(fromLen, numberLen.subtract(new BigInteger(""+i)))
                    ) 
                );
            }
            return ""+retval;
        }

        String base10 = fromBaseInput.equals("0123456789") ? number : convBase(number, fromBaseInput, "0123456789");

        if(new BigInteger(base10).compareTo(toLen) < 0)
            return ""+toBaseInput.charAt(Integer.parseInt(base10));

        String retVal = "";
        BigInteger base10bigInt = new BigInteger(base10);
        while(!base10bigInt.equals(BigInteger.ZERO)){
            retVal = toBaseInput.charAt(base10bigInt.mod(toLen).intValue()) + retVal;
            base10bigInt = base10bigInt.divide(toLen); 
        }
        return ""+retVal;
    }

    public static void main(String[] args) {
        String number = "98765;43210;9999;2";
        String fromBase = "0123456789;";
        String toBase = "0123456789ABCDEFGHIJK";

        String converted = ConvBase.convBase(number, fromBase, toBase);
        String back = ConvBase.convBase(converted, toBase, fromBase);

        System.out.println("Number = "+number);
        System.out.println("Converted = "+converted);
        System.out.println("Back = "+back); 

        System.exit(0);
    }
}
Number = 998765;43210;9999;2
Converted = GJK7K6B2KKGKK96
Back = 998765;43210;9999;2