Latest Articles
 by awuehr on 11-10-2008 in Tips & Tricks by Village Idiot on 11-04-2008 in Classes & Objects by codefreek on 10-23-2008 in Basic by wiifanatic on 09-12-2008 in Security & Permissions by drewbee on 07-03-2008 in Tips & Tricks
 IRC Channel
 Join the friendly bunch on IRC... (#TalkPHP on Freenode) ...Also available via a web interface. See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
 Associates
 Associates
 TalkPHP » to decimal conversion problem
 10-13-2009, 06:19 PM #1 (permalink) 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

 Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On

 Similar Threads Thread Thread Starter Forum Replies Last Post Decimal -> Hexidecimal ASCII Conversion ioan1k Advanced PHP Programming 1 10-06-2009 02:02 PM Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM Huge Session Problem Killswitch General 1 11-17-2008 02:36 AM Problem with Query + While \$a = mysql_fetch_array(\$b) Aaron Absolute Beginners 0 07-03-2008 07:16 PM A bug or a cache problem?! yazid Advanced PHP Programming 0 05-22-2008 08:40 AM

All times are GMT. The time now is 05:28 PM.