<?
/********************************************************************************
*HEADERING
*/
class bin2array{
var $abin; //array representation (BE)
var $sbin; //string binary
var $hbin; //string hexa
var $ibin; //integer
var $err;
var $errnum;
var $errdesc;
var $version="0.0";
function emsgerror($n,$desc){
$err=1;
$errnum=$n;
$errdesc=$desc;
}
function erange($val){
$fret=1;
if(($val==1||$val==0)){
$fret=0;
$this->emsgerror(0,"Bit fuera de rango");
}
return $fret;
}
function ehex($v){
//HEX-INT
switch(strtolower($v)){
case "f":
$iv=15;
break;
case "e":
$iv=14;
break;
case "d":
$iv=13;
break;
case "c":
$iv=12;
break;
case "b":
$iv=11;
break;
case "a":
$iv=10;
break;
default:
$iv=$v;
}
return $iv;
}
function etable_hex2bin($hstr){
//recibe una cadena en hexa y la traduce a binario en cadena
$vi=0;
for($i=0;$i<strlen($hstr);$i++){
$vi+=$this->ehex($hstr[$i])*(pow(16,strlen($hstr)-1-$i));
}
return sprintf("%b",$vi);
}
function emake_arr($bstr){
//recibe una cadena binaria, y debe retornar un array
//return split("",$bstr);
$a=array();
for($i=0;$i<strlen($bstr);$i++){
$a[$i]=$bstr[$i];
}
return $a;
}
function etable_bin2hex($bstr){
//traduce los binarios a hexa
$vi=0;
$vi=$this->etable_bin2int($bstr);
return sprintf("%X",$vi);
}
function etable_bin2int($bstr){
//traduce los binarios a enteros
$vi=0;
for($i=0;$i<strlen($bstr);$i++){
$vi+=$bstr[$i]*(pow(2,strlen($bstr)-1-$i));
}
return $vi;
}
function emake_str($abin){
//Recibe un array con la representacion binaria
$vs="";
for($i=0;$i<count($abin);$i++)
$vs.=(($abin[$i])?"1":"0");
return $vs;
}
function echeck_arr($parr){
for($i=0;$i<count($parr);$i++){
if($parr[$i]){
$parr[$i]=1;
}else{
$parr[$i]=0;
}
}
return 1;
}
function etraduce($from,$dat){
$fret=1;
switch($from){
case "array":
if($this->echeck_arr(&$dat)){
$this->abin=$dat;
$this->sbin=$this->emake_str($this->abin);
$this->hbin=$this->etable_bin2hex($this->sbin);
$this->ibin=$this->etable_bin2int($this->sbin);
}else{
$this->emsgerror(2,"El array posee valores invalidos. 01");
$fret=0;
}
break;
case "integer":
$this->sbin=sprintf("%b",$dat);
$this->hbin=$this->etable_bin2hex($this->sbin);
$this->ibin=$this->etable_bin2int($this->sbin);
$this->abin=$this->emake_arr($this->sbin);
break;
case "double":
$this->etraduce(gettype(intval($dat)),intval($dat));
break;
case "boolean":
$dat=($dat)?1:0;
$this->sbin=($dat)?"1":"0";
$this->hbin=($dat)?"1":"0";
$this->ibin=$dat;
$this->abin[0]=$dat;
break;
case "string":
if(eregi("[^0-9a-f]",$dat)){
$this->emsgerror(2,"La cadena contien valores errones.0-9a-f");
$fret=0;
}else{
$dat=eregi_replace("[h-z]","",$dat);
if(eregi("[^0-9]",$dat)){
$this->hbin=$dat;
$this->sbin=$this->etable_hex2bin($dat);
}elseif(eregi("[^0-1]",$dat)){
$this->ibin=intval($dat);
$this->sbin=sprintf("%b",$dat);
$this->hbin=$this->etable_bin2hex($this->sbin);
$this->ibin=$this->etable_bin2int($this->sbin);
$this->abin=$this->emake_arr($this->sbin);
}else{
$this->hbin=$this->etable_bin2hex($dat);
$this->sbin=$dat;
}
$this->ibin=$this->etable_bin2int($this->sbin);
$this->abin=$this->emake_arr($this->sbin);
}
break;
case "object":
if(!(@$dat->isBin2Array())){
$this->emsgerror(1,"Inicializacion erronea");
$fret=0;
}else{
$this->abin=$dat->abin;
$this->sbin=$dat->sbin;
$this->hbin=$dat->hbin;
$this->ibin=$dat->ibin;
}
break;
default:
case "resource":
$this->emsgerror(1,"Inicializacion erronea");
$fret=0;
break;
}
return $fret;
}
function egetinteger($dat){
$otmp=new bin2array($dat);
$ival=$otmp->ibin;
$otmp->destroy();
unset($otmp);
return $ival;
}
function op($opcode,$data){
switch(strtolower($opcode)){
case "add":
$this->add($data);
break;
case "mul":
$this->mul($data);
break;
case "div":
$this->div($data);
break;
case "nul":
return 0;
break;
default:
}
}
function add($dat){
$ival=$this->egetinteger($dat);
$this->etraduce(gettype($this->ibin+$ival),$this->ibin+$ival);
}
function mul($dat){
$ival=$this->egetinteger($dat);
$this->etraduce(gettype($this->ibin*$ival),$this->ibin*$ival);
}
function div($dat){
$ival=$this->egetinteger($dat);
$this->etraduce(gettype($this->ibin/$ival),$this->ibin/$ival);
}
function sub($dat){
$ival=$this->egetinteger($dat);
$this->etraduce(gettype($this->ibin-$ival),$this->ibin-$ival);
}
function isBin2Array(){
return 1;
}
function bin2array($p1){
$fret=1;
$fret=$this->etraduce(gettype($p1),$p1);
return $fret;
}
function destroy(){
return 1;
}
}
/***********************************************************************
*[04/06/2002]-Roberto, Celestino. <rcelestino@claxson.com>
*
* Name: bin2array
*/
/***********************************************************************
* EXAMPLE SECTION
*/
/*
$sb1= new bin2array("11111111"); //initialize with a string of a number in binary representation
$sb1= new bin2array("255"); //initialize with a string of decimal number
$sb1= new bin2array("FF"); //initialize with a string of hexa number
$sb1= new bin2array($sb3); //copy constructor
$sb1= new bin2array(true); //initialize with a simple value as boolean variable 1:0
$sb1= new bin2array(array(1,1,1,1,1,1,1,1)); //initialize with an array of binary M7....M0 (BE)
$sb1= new bin2array(15.5); //initialize with a float value (this will be casted to integer)
$sb1->add(15); //add a decimal value
$sb1->add("FF"); //add, in hexa string
$sb1->add("15"); //add, in decimal string
$sb1->add("1111"); //add, in binary string
$sb1->add($sb1); //add, two objects
$sb1->op("add","15"); //operation main
$sb1->destroy(); //destructor, call for convention (remember unset($nameobj);)
*/
?>
|