Codice PHP:
<?php
class baseX {
protected $arrChr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','Q','T','U','V','W','X','Y','Z');
protected $arrOrd;
function __construct() {
$this->arrOrd = array_flip($this->arrChr);
} // function __construct()
//===== Ordine della lettera
private function ordL($letter) {
$temp = $this->arrOrd[$letter];
return $temp;
} // private function ordL($letter)
//===== Carattere del numero
private function chrL($numero) {
$temp = $this->arrChr[$numero];
return $temp;
} // private function chrL($numero)
//===== Trova la posizione della lettera
private function charPosition($code) {
$len = strlen($code);
$ind = 0;
$pos = -1;
while ($ind < $len) {
if (!is_numeric(substr($code,$ind,1))) {
$pos = $ind;
}
$ind++;
} // while ($ind < $len)
if ($pos == -1) {
return null;
} else {
return $pos;
} // if ($pos == -1)
} // function decodeX($code)
//===== decodifica un numero codificato in base 10
function decodeBaseX($code) {
$pos = $this->charPosition($code);
if ( $pos === null) {
return null;
} // if ($this->charPosition($code) === null)
$letter = substr($code,$pos,1);
$resto = str_replace($letter,'',$code);
$th = $this->ordL($letter);
return ($th * 4000) + ($pos * 1000) + $resto;
} // function decodeBaseX($code)
//===== codifica un numero in base 10
function codeBaseX($number) {
if ($this->charPosition($number) !== null) {
return null;
} // if ($this->charPosition($number) !== null)
$th = (int) ($number / 1000);
$resto = $number - ($th * 1000);
$restoF = str_repeat('0',3-strlen($resto)).$resto;
$tx = (int) ($th / 4);
$pos = $th % 4;
$letter = $this->chrL($tx);
if ($pos == 0) {
$return = $letter.$restoF;
} else {
if ($pos == 1) {
$return = substr($restoF,0,1).$letter.substr($restoF,1,2);
} else {
if ($pos == 2) {
$return = substr($restoF,0,2).$letter.substr($restoF,2,1);
} else {
$return = $restoF.$letter;
} // if ($pos == 2)
} // if ($pos == 1)
} // if ($pos == 0)
return $return;
} // function codeBaseX($number)
//===== aggiunge due "numeri"
function add($element1,$element2) {
if ($this->charPosition($element1) === null) {
$add_element1 = $element1;
} else {
$add_element1 = $this->decodeBaseX($element1);
} // if (charPosition($element1) === null)
if ($this->charPosition($element2) === null) {
$add_element2 = $element2;
} else {
$add_element2 = $this->decodeBaseX($element2);
} // if (charPosition($element2) === null)
$inter = $add_element1 + $add_element2;
return $this->codeBaseX($inter);
} // function add($element1,$element2)
} // class baseX
class baseX_tipo1 extends baseX {
protected $arrChr = array('A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','Q','T','U','V','W','X');
} // class baseX_tipo1 extends baseX
class baseX_tipo2 extends baseX {
protected $arrChr = array('J','K','W','X','Z');
} // class baseX_tipo2 extends baseX
$a = new baseX();
print "Code :<br/>";
print $a->codeBaseX(15)."<br/>";
print $a->codeBaseX(999)."<br/>";
print $a->codeBaseX(3400)."<br/>";
print $a->codeBaseX(1999)."<br/>";
print $a->codeBaseX(2000)."<br/>";
print $a->codeBaseX(3000)."<br/>";
print $a->codeBaseX(4000)."<br/>";
print $a->codeBaseX(5000)."<br/>";
print "Decode :<br/>";
print $a->decodeBaseX('4D00')."<br/>";
print $a->decodeBaseX('8P99')."<br/>";
print "Add :<br/>";
for ($i=0;$i<100;$i++) {
print $a->add('9D50',$i)."<br/>";
} // for ($i=0;$i<100;$i++)
print $a->codeBaseX(17128)."<br/>";
print $a->decodeBaseX('25I6')."<br/>";
print $a->add('1E28','1E28')."<br/>";
print "<hr/>";
print "Tipologia 1 :<br/>";
$b = new baseX_tipo1();
print $b->codeBaseX(999)."<br/>";
print "<hr/>";
print "Tipologia 2 :<br/>";
$c = new baseX_tipo2();
print $c->codeBaseX(999)."<br/>";
?>