PHP Realized 62 to 10 10 to 62 to 62 Function Examples

  • 2021-12-12 03:58:24
  • OfStack

In this paper, the functions from 62 to 10 and from 10 to 62 realized by PHP are described with examples. Share it for your reference, as follows:

Baidu can't use it, so I have to write it myself. Welcome to leave a message where there is bug. Thank you


function from62to10($str){
  $dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $len = strlen($str);
  $dec = 0;
  for($i = 0;$i<$len;$i++){
    // Find the subscript of the corresponding dictionary 
    $pos = strpos($dict, $str[$i]);
    $dec += $pos*pow(62,$len-$i-1);
  }
  return $dec;
}
echo from62to10('6r2HqO');
echo '<br>';
$dec = '5896401674';
function from10to62($dec) {
  $dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $result = '';
  do {
    $result = $dict[$dec % 62] . $result;
    $dec = intval($dec / 62);
  } while ($dec != 0);
  return $result;
}
echo from10to62($dec);

Run results:

5896401674
6r2Hq1

PS: Here are some calculation and conversion tools recommended for your reference:

Online arbitrary binary conversion tool:
http://tools.ofstack.com/transcoding/hexconvert

Scientific calculator online _ advanced calculator online calculation:
http://tools.ofstack.com/jisuanqi/jsqkexue

Online Calculator _ Standard Calculator:
http://tools.ofstack.com/jisuanqi/jsq

For more readers interested in PHP related contents, please check the topics of this site: "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary" and "PHP Mathematical Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: