Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

baseX converter (See related posts)

you can use this function to convert an integer into any number system upto base36 (e.g. for TinyURL generation coupled with a global ID counter)

function int2baseX($val,$base=16) {
  if (0==$val) return 0;
  $symbols='0123456789abcdefgihjklmnopqrstuvwxyz';
  $result='';
  $exp=$oldpow=1;
  while($val>0 && $exp<10) {
    $pow=pow($base,$exp++);
    $mod=($val % $pow);
    $result=substr($symbols,$mod/$oldpow,1).$result;
    $val-=$mod;
    $oldpow=$pow;
  }
  return $result;
}

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts