View Single Post
 10-13-2009, 06:19 PM #1 (permalink) adham is me The Wanderer     Join Date: May 2009 Posts: 19 Thanks: 4 to decimal conversion problem 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: ``` _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 .="".\$nums_array[\$i] ."" ."*"."( \$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 "(".\$this->_final_solution.")"."".\$this->_base."";     }     public function getStringSolution(){         return \$this->_string_solution;     } } ?> ``` & this is the index.php file that makes use of this class PHP Code: ``` toDecimal(); echo \$obj->getFinalSolution(); echo "
"; echo \$obj->getStringSolution(); echo"
"; //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 ! __________________ I love you NASSER