hi everyone
i tried to make my own software to convert between numerical systems ( binary - decimal - octal - hexadecimal ), when i made my first method " toDecimal " it worked fine with numbers with short length like 100,1001,11101 but when i wrote 011111110 or like-numbers it started to output strangely !
i designed this program to echo not only the final solution , but also solution steps ( i wrote it to solve the huge number of sums that is required to be solved in the faculty !! ) .
Here is the code
PHP Code:
<?php
//classes/adhamConversions.class.php
class adhamConvertions{
// number 2 b converted
protected $_num;
// num type ('binary','decimal','octal','hexadecimal')
protected $_type;
protected $_final_solution;
protected $_base,$_string_solution;
protected $_domain;
// set the number that will be converted to other types .
public function __construct($num,$type){
$this->_type = $type;
$this->_num = $num;
$this->_whatIsTheBase();
}
// determining the base
protected function _whatIsTheBase(){
// determining the base
switch($this->_type){
case 'binary':
$this->_base = 2;
$this->_domain = array(0,1); // system numbers
break;
case 'octal' :
$this->_base = 8;
$this->_domain = array(0,1,2,3,4,5,6,7);
break;
case 'hexadecimal':
$this->_base = 16;
break;
default :
echo 'you have to specify your number\'s type !';
}
}
// convert the pre-set number to decimal
public function toDecimal(){
// spliting our number into array
$nums_array = array_reverse(str_split($this->_num)) ;
$final_solution = 0 ;
$final_string = '';
$nums_nums = count($nums_array)-1;//2
for($i=0;$i<=$nums_nums;$i++){
$final_solution = $nums_array[$i]*pow(($this->_base),$i) + $final_solution;
$final_string .="<b>".$nums_array[$i] ."</b>" ."*"."( $this->_base )"."^".$i;
if($i!==$nums_nums){
$final_string .=" + ";
}
//print_r($nums_array);
}
$this->_string_solution = $final_string;
$this->_final_solution = $final_solution;
}
// returns the last solution
public function getFinalSolution(){
return "<b style='font-size:30px'>(</b>".$this->_final_solution."<b style='font-size:30px'>)</b>"."<i style='font-size:10px'>".$this->_base."</i>";
}
public function getStringSolution(){
return $this->_string_solution;
}
}
?>
& this is the index.php file that makes use of this class
PHP Code:
<?php
require 'classes/adhamConversions.class.php';
$obj = new adhamConvertions(101111111111111,'binary');
$obj->toDecimal();
echo $obj->getFinalSolution();
echo "<br>";
echo $obj->getStringSolution();
echo"<hr>";
//echo bindec(101);
?>
& this is the output when the script runs !
Code:
(327670)2//i know 2 is wrong but this is not the main problem !
4*( 2 )^0 + 1*( 2 )^1 + +*( 2 )^2 + E*( 2 )^3 + 1*( 2 )^4 + 1*( 2 )^5 + 1*( 2 )^6 + 1*( 2 )^7 + 1*( 2 )^8 + 1*( 2 )^9 + 1*( 2 )^10 + 1*( 2 )^11 + 1*( 2 )^12 + 1*( 2 )^13 + 1*( 2 )^14 + 1*( 2 )^15 + 0*( 2 )^16 + .*( 2 )^17 + 1*( 2 )^18
from where comes 4 , E and like-numbers that are not exist it the input number , which was 101111111111111 ??
waiting
thanks for reading all of this !