title: Converting base10 to base62 and back in PHP on a 32bit system
body: <p>I found a previously asked question <a href="https://stackoverflow.com/questions/4964197/converting-a-number-base-10-to-base-62-a-za-z0-9">here</a>, and am trying to implement <a href="https://stackoverflow.com/a/4964352/18762901">Eineki's solution,</a> however I'm on a 32bit system (I've installed Ubuntu 20.04 on an old laptop for testing).  It was noted in the comments that the code wouldn't work on a 32bit system but there are workarounds, but they are not spelled out clearly enough for me.  Unfortunately I'm pretty new with PHP and also with this website.  I tried to comment there to ask for help and was told I couldn't because my account is new.</p>
  <p>So here's how I modified that code, based on the instructions given in those comments.  The instructions were:</p>
  <blockquote>
  <p>Try to change the line occurence of $num % $b with fmod($num, $b) and
  $num/$b (and similar ones) with intdiv($nub, $b).</p>
  </blockquote>
  <pre><code>function base10to62($num, $b=62) {
    $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $r = fmod($num  % $b) ;
    $res = $base[$r];
    $q = floor(intdiv($num/$b));
    while ($q) {
      $r = fmod($q % $b);
      $q = floor(intdiv($q/$b));
      $res = $base[$r].$res;
    }
    return $res;
  }
  
  function base62to10( $num, $b=62) {
    $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $limit = strlen($num);
    $res=strpos($base,$num[0]);
    for($i=1;$i&lt;$limit;$i++) {
      $res = $b * $res + strpos($base,$num[$i]);
    }
    return $res;
  
  $base3_string1 = &quot;212012222102200121211101010110220202222222211121102101011101111110021111210000102211101010101110101010101010220110000110111010101010000010002020001010200000022111111111110011120201111110000002002002002001011011101010000110010000000000&quot;;
  $base10test = base_convert($base3_string1, 3, 10);
  $base62test = base10to62($base10test);
  $base62returntest = base62to10($base62test);
  
  echo (&quot;My base3_string1 is: &quot;.$base3_string1.&quot;&lt;br&gt;&quot;);
  echo (&quot;My base10 number is: &quot;.$base10test.&quot;&lt;br&gt;&quot;);
  echo (&quot;My base62 number is: &quot;.$base62test.&quot;&lt;br&gt;&quot;);
  echo (&quot;My base62return hash is: &quot;.$base62returntest.&quot;&lt;br&gt;&quot;);
  </code></pre>
  <p>As you can see, I have a base3 number I'm converting to base10.  I'm doing this because it looks like Eineki's function wants base10 input.  The base10 number I'm trying to convert is 8080064204040204462880280000286008662604880682228402402248426842, but when I attempt this the return is 0 and when I try to change it back of course the result is 0 because the input is 0.</p>
  <p>I'm guessing I've misinterpreted Eineki's instructions on how to convert his code to work on a 32bit system.  If anyone can help me correct this it would be appreciated.</p>