note 106217 deleted from function.base-convert by danbrown

From: Date: Wed, 19 Oct 2011 13:08:13 +0000
Subject: note 106217 deleted from function.base-convert by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183996@lists.php.net to get a copy of this message
Note Submitter: Mirko Cap ---- The example below don't works. <?php function intToAlphaBaseN($n,$baseArray) { $l=count($baseArray); $s = ''; for ($i = 1; $n >= 0 && $i < 10; $i++) { $s = $baseArray[($n % pow($l, $i) / pow($l, $i - 1))].$s; $n -= pow($l, $i); } return $s; } function alphaBaseNToInt($nab,$baseArray) { $l = count($baseArray); $b = strlen($nab); $s = 0; $values = array(); foreach ($baseArray as $key => $value) $values[$value] = $key ; for ($i = 0; $i < $b; $i++) $s += ($values[$nab[$i]] * pow($l, $b - $i - 1)); return $s; } $alpha = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ); $r=rand(0, 99999999999);//if we consider 426596311 as input number echo($r = intToAlphaBaseN($r,$alpha));// returns a different number also if the base is the same echo(alphaBaseNToInt($r,$alpha));// reverse function works ?>

« previous php.notes (#183996) next »