1

php only have 36 base converter, i need more than that, i found the code but it in js, can anyone help me translate it to php?

thanks.

    function baseConverter (number,ob,nb) {
        // Created 1997 by Brian Risk.  http://brianrisk.com
        //number = number.toUpperCase();
        var list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        var dec = 0;
        if(number=="0" || number=="" || number==0){
            number="0";
        }else{
            for (var i = 0; i <=  number.length; i++) {
                dec += (list.indexOf(number.charAt(i))) * (Math.pow(ob , (number.length - i - 1)));
            }
            number = "";
            var magnitude = Math.floor((Math.log(dec))/(Math.log(nb)));
            for (var i = magnitude; i >= 0; i--) {
                var amount = Math.floor(dec/Math.pow(nb,i));
                number = number + list.charAt(amount); 
                dec -= amount*(Math.pow(nb,i));
            }
        }
        return number;
    }
5
  • 1
    Note, please read the FAQ. This question is not suited for StackOverflow. If you change the question to something alike "Is there a base converter, for bases higher than 36, for PHP?", you probably get better answers too.
    – Pindatjuh
    Aug 19, 2011 at 13:42
  • 1
    If you're just looking for a base 64 encoder: php.net/manual/en/function.base64-encode.php
    – dtanders
    Aug 19, 2011 at 14:14
  • @dtanders thanks for the link, but those base64 encoder make the string much longer, my program only processing base10 numbers encoded to custom base62, make it shorter for url and easier to process
    – deval
    Aug 21, 2011 at 12:53
  • @Pindatjuh hmn... its not just base converter i need those custom alphabeth too.. :D
    – deval
    Aug 21, 2011 at 12:54
  • 1
    I was actually trying to find a JS converter. Thanks!
    – whamsicore
    Jan 12, 2012 at 20:01

1 Answer 1

3
 function baseConverter ($number,$ob,$nb) {
        $list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        $dec = 0;
        $number = (string)$number;
        if(empty($number)){
            $number="0";
        }
        else
        {
            $number_len  = strlen($number); 
            for ($i=0; $i<$number_len; $i++)
            {
                $dec += strpos($list, $number[$i])*(pow($ob, ($number_len-$i-1)));
            }
            $number = '';
            $magnitude = floor(log($dec)/log($nb));
            for($i=$magnitude; $i>=0; $i--)
            {
                $amount = floor($dec/pow($nb, $i));
                $number = $number.$list[$amount];
                $dec-= $amount*(pow($nb, $i));
            }
        }
        return $number;
     }

    var_dump( baseConverter("789", 10, 45));    

  will return string(2) "HO" 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.