TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble 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
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-13-2009, 06:19 PM   #1 (permalink)
The Wanderer
 
adham is me's Avatar
 
Join Date: May 2009
Posts: 19
Thanks: 4
adham is me is on a distinguished road
Calculator 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:
<?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 ;
        
$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 !
__________________
I love you NASSER
adham is me is offline  
Reply With Quote
Old 10-13-2009, 08:58 PM   #2 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

Why not use

http://php.net/decbin
http://php.net/decoct
http://php.net/dechex

http://php.net/bindec
http://php.net/octdec
http://php.net/hexdec

Save yourself the headache and not reinvent the wheel :)
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 10-13-2009, 09:29 PM   #3 (permalink)
The Wanderer
 
adham is me's Avatar
 
Join Date: May 2009
Posts: 19
Thanks: 4
adham is me is on a distinguished road
Default

thanks for replying

but i want a way to convert and write conversion steps as well , so i can print it and save myself writing it manually , i want it as a report in the university !

we have to convert more than 48 sums !!!!
__________________
I love you NASSER
adham is me is offline  
Reply With Quote
Old 10-15-2009, 12:08 PM   #4 (permalink)
The Wanderer
 
adham is me's Avatar
 
Join Date: May 2009
Posts: 19
Thanks: 4
adham is me is on a distinguished road
Default

the error was to forget to quote the number , because we treat it as string not number !!
__________________
I love you NASSER
adham is me is offline  
Reply With Quote
Old 10-19-2009, 09:16 PM   #5 (permalink)
The Wanderer
 
bucabay's Avatar
 
Join Date: Oct 2009
Location: Fiji
Posts: 6
Thanks: 0
bucabay is on a distinguished road
Default

Quote:
Originally Posted by ioan1k View Post
I understand you're writing your own. But for anyone's reference, the base_convert() function can convert any base between 2 and 36 (0-9a-z).

http://php.net/manual/en/function.base-convert.php

bucabay is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

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 Off
Trackbacks 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 09:25 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design