converting a number base 10 to base 62 (a-zA-Z0-9)

后端 未结 10 1699
挽巷
挽巷 2020-11-29 01:22

I have a number in base 10. Is there anyway to translate it to a base 62?

Example:

echo convert(12324324);
// returns Yg3 (fantasy example here)
         


        
10条回答
  •  悲&欢浪女
    2020-11-29 01:59

    A simpler (and possibly faster) implementation that does not use pow nor log:

    function base62($num) {
      $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      $res = '';
      do {
        $res = $index[$num % 62] . $res;
        $num = intval($num / 62);
      } while ($num);
      return $res;
    }
    

提交回复
热议问题