This tutorial ,I have included complete number converter.Interface as follows.
Number | ||
Operation:- | Binary to Decimal | Binary to Octal |
Binary to Hexadecimal | Decimal to Binary | |
Decimal to Octal | Decimal to Hexadecimal | |
Octal to Binary | Octal to Decimal | |
Octal to hexadecimal | Hexadecimal to binary | |
Hexadecimal to Decimal | Hexadecimal to Octal | |
PHP codes are bellow
<table bgcolor=#d38a2a> <form name="calc" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <tr bgcolor=#ffffff><td>Number </td><td><input type="text" name="value1"><td align="center"><img src="cal.png"></td></tr> <tr bgcolor=#ffffff><td>Operation:-</td><td><input type="radio" name="oper" value="bdec">Binary to Decimal</td><td><input type="radio" name="oper" value="boct">Binary to Octal</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="bhex">Binary to Hexadecimal</td><td><input type="radio" name="oper" value="dbin">Decimal to Binary</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="doct">Decimal to Octal</td><td><input type="radio" name="oper" value="dhex">Decimal to Hexadecimal</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="obin">Octal to Binary</td><td><input type="radio" name="oper" value="odec">Octal to Decimal</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="ohex">Octal to hexadecimal</td><td><input type="radio" name="oper" value="hbin">Hexadecimal to binary</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="hdec">Hexadecimal to Decimal</td><td><input type="radio" name="oper" value="hoct">Hexadecimal to Octal</input></td></tr> <tr bgcolor=#ffffff><td colspan=3 align="right"><input type="submit" value="Calculate"></td></tr> </form> </table> <?php $number1=$_POST['value1']; $oper=$_POST['oper']; if($oper=='bdec'){ $bin="$number1"; $dec= base_convert($bin,2,10); echo "<font size=6 color='blue'><b>".$dec."</b></font>"; } else if($oper=='boct'){ $bin="$number1"; $oct= base_convert($bin,2,8); echo "<font size=6 color='blue'><b>".$oct."</b></font>"; } else if($oper=='bhex'){ $bin="$number1"; $hex= base_convert($bin,2,16); echo "<font size=6 color='blue'><b>". $hex."</b></font>"; } else if($oper=='dbin'){ $dec="$number1"; $bin= base_convert($dec,10,2); echo "<font size=6 color='blue'><b>". $bin."</b></font>"; } else if($oper=='doct'){ $dec="$number1"; $oct= base_convert($dec,10,8); echo "<font size=6 color='blue'><b>". $oct."</b></font>"; } else if($oper=='dhex'){ $dec="$number1"; $hex= base_convert($dec,10,16); echo "<font size=6 color='blue'><b>". $hex."</b></font>"; } else if($oper=='obin'){ $oct="$number1"; $bin= base_convert($oct,8,2); echo "<font size=6 color='blue'><b>". $bin."</b></font>"; } else if($oper=='odec'){ $oct="$number1"; $dec= base_convert($oct,8,10); echo "<font size=6 color='blue'><b>". $dec."</b></font>"; } else if($oper=='ohex'){ $oct="$number1"; $hex= base_convert($oct,8,16); echo "<font size=6 color='blue'><b>". $hex."</b></font>"; } else if($oper=='hbin'){ $hex="$number1"; $bin= base_convert($hex,16,2); echo "<font size=6 color='blue'><b>". $bin."</b></font>"; } else if($oper=='hdec'){ $hex="$number1"; $dec= base_convert($hex,16,10); echo "<font size=6 color='blue'><b>". $dec."</b></font>"; } else if($oper=='hoct'){ $hex="$number1"; $oct= base_convert($hex,16,8); echo "<font size=6 color='blue'><b>". $oct."</b></font>"; } else { echo "<font color='red'>Wrong Input or Method</font>"; } ?> |
No comments:
Post a Comment