Saturday, March 15, 2014
Midsize Program
The following is a simple Binary to decimal converter program written in PHP.
Please note that the Binary input is hard coded, as this blog has yet to cover user inputs within PHP.
However, the program will run and execute for any Binary number that is written in.
<?php
$BinaryNumber="1010";
$StringLength=strlen($BinaryNumber);
$DecimalTotal=0;
$NumericvalueofDigit=0;
for ($i=1; i<=$StringLength; $i++){
$BinaryDigit=substr($BinaryNumber, ($i*-1),1);
$NumericvalueofDigit=((int)$BinaryDigit)*pow(2,($i-1));
$DecimalTotal=$DecimalTotal + $NumericvalueofDigit;
}
echo $DecimalTotal;
?>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment