title: Converting from base X to base Y in JAVA is returning different characters than the same function in PHP
body: <p>I've created a method in JAVA in order to do the same thing of an existing PHP function, that is: convert an arbitrarily large number from any base to any base.</p>
  
  <p>The java method is working fine and I can convert numbers from one base to another and then convert it back, but the resulted strings are different from the PHP function. This is a problem to me, because I want to convert a number in PHP and then convert it back in JAVA.</p>
  
  <p>For example, lets convert the number <code>998765;43210;9999;2</code> from Base11 with alphabet <code>0123456789;</code> to Base21 with alphabet <code>0123456789ABCDEFGHIJK</code> in PHP and JAVA:</p>
  
  <p>Result of the example in PHP:</p>
  
  <pre><code>convBase("998765;43210;9999;2", "0123456789;", "0123456789ABCDEFGHIJK") = "GJK7K6B2KKGKK96"
  </code></pre>
  
  <p>Result of the example in JAVA:</p>
  
  <pre><code>convBase("998765;43210;9999;2", "0123456789;", "0123456789ABCDEFGHIJK") = "1B0EJAJ0IG3DABI"
  </code></pre>
  
  <p>I would like the results to be the same, so I could convert a number in PHP and convert it back in JAVA.</p>
  
  <p>I think that the problem can be character encoding, but I don't know how to solve it.</p>
  
  <p><strong>PHP function and test:</strong></p>
  
  <pre><code>&lt;?php
      function convBase($numberInput, $fromBaseInput, $toBaseInput)
      {
          if ($fromBaseInput==$toBaseInput) return $numberInput;
          $fromBase = str_split($fromBaseInput,1);
          $toBase = str_split($toBaseInput,1);
          $number = str_split($numberInput,1);
          $fromLen=strlen($fromBaseInput);
          $toLen=strlen($toBaseInput);
          $numberLen=strlen($numberInput);
          $retval='';
          if ($toBaseInput == '0123456789')
          {
              $retval=0;
              for ($i = 1;$i &lt;= $numberLen; $i++)
                  $retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));
                  return $retval;
          }
          if ($fromBaseInput != '0123456789')
              $base10=convBase($numberInput, $fromBaseInput, '0123456789');
              else
              $base10 = $numberInput;
              if ($base10&lt;strlen($toBaseInput))
                  return $toBase[$base10];
                  while($base10 != '0')
                  {
                      $retval = $toBase[bcmod($base10,$toLen)].$retval;
                      $base10 = bcdiv($base10,$toLen,0);
                      }
                      return $retval;
      }
  
      header('Content-Type: text/html; charset=utf-8');
  
      $number = "998765;43210;9999;2";
      $fromBase = "0123456789;";
      $toBase = "0123456789ABCDEFGHIJK";
  
      $converted = convBase($number, $fromBase, $toBase);
      $back = convBase($converted, $toBase, $fromBase);
  
      echo "Number: ".$number."&lt;br&gt;";
      echo "Converted: ".$converted."&lt;br&gt;";
      echo "Back: ".$back."&lt;br&gt;";
  ?&gt;
  </code></pre>
  
  <p><strong>JAVA method and test:</strong></p>
  
  <pre><code>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&lt;=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) &lt; 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);
      }
  }
  </code></pre>