Wednesday, November 4, 2009

PHP script – Number System Converter

We know that there are various kinds of bases (binary, decimal etc.) in number system in Mathematics. Four of them are most useful. So I have created a PHP script for converting binary, octal, decimal and hexadecimal to binary, octal, decimal and hexadecimal. The script is given below for sharing with you:



<html>
<head>
<title>Base Converter</title>
</head>

<body>
<form action="binary.php" method="get">
<input type="text" name="num" />
<br />

From: <select name="from">
        <option value="binary" selected="selected">Binary</option>
     <option value="octal">Octal</option>
<option value="decimal">Decimal</option>
<option value="hexadecimal">Hexadecimal</option>
</select>

<br />

To: <select name="to">
        <option value="binary" selected="selected">Binary</option>
        <option value="octal">Octal</option>
        <option value="decimal">Decimal</option>
        <option value="hexadecimal">Hexadecimal</option>
</select>

<br />
The Converted number is:

<?php

if ($_GET['from'] == 'binary' && $_GET['to'] == 'binary' ){

    $a = base_convert($_GET['num'], 2, 2);

    print $a;

} else if ($_GET['from'] == 'binary' && $_GET['to'] == 'octal' ){

    $a = base_convert($_GET['num'], 2, 8);

    print $a;

} else if ($_GET['from'] == 'binary' && $_GET['to'] == 'decimal' ){

    $a = base_convert($_GET['num'], 2, 10);

    print $a;

} else if ($_GET['from'] == 'binary' && $_GET['to'] == 'hexadecimal' ){

    $a = base_convert($_GET['num'], 2, 16);

    print $a;

} else if ($_GET['from'] == 'octal' && $_GET['to'] == 'binary' ){

    $a = base_convert($_GET['num'], 8, 2);

    print $a;

} else if ($_GET['from'] == 'octal' && $_GET['to'] == 'octal' ){

    $a = base_convert($_GET['num'], 8, 8);

    print $a;

} else if ($_GET['from'] == 'octal' && $_GET['to'] == 'decimal' ){

    $a = base_convert($_GET['num'], 8, 10);

    print $a;

} else if ($_GET['from'] == 'octal' && $_GET['to'] == 'hexadecimal' ){

    $a = base_convert($_GET['num'], 8, 16);

    print $a;

} else if ($_GET['from'] == 'decimal' && $_GET['to'] == 'binary' ){

    $a = base_convert($_GET['num'], 10, 2);

    print $a;

} else if ($_GET['from'] == 'decimal' && $_GET['to'] == 'octal' ){

    $a = base_convert($_GET['num'], 10, 8);

    print $a;

} else if ($_GET['from'] == 'decimal' && $_GET['to'] == 'decimal' ){

    $a = base_convert($_GET['num'], 10, 10);

    print $a;

} else if ($_GET['from'] == 'decimal' && $_GET['to'] == 'hexadecimal' ){

    $a = base_convert($_GET['num'], 10, 16);

    print $a;

} else if ($_GET['from'] == 'hexadecimal' && $_GET['to'] == 'binary' ){

    $a = base_convert($_GET['num'], 16, 2);

    print $a;

} else if ($_GET['from'] == 'hexadecimal' && $_GET['to'] == 'octal' ){

    $a = base_convert($_GET['num'], 16, 8);

    print $a;

} else if ($_GET['from'] == 'hexadecimal' && $_GET['to'] == 'decimal' ){

    $a = base_convert($_GET['num'], 16, 10);

    print $a;

} else {

    $a = base_convert($_GET['num'], 16, 16);

    print $a;

}

?>


<br />
<input type="submit" name="submit" value="Convert" />
<input type="reset" name="reset" value="Reset" />
</form>
</body>
</html>

You can also download the script from this link.

No comments:

Post a Comment