PHP base_convert

I wrote a simple tool to visualize numbers in different bases to help solve a math problem I was thinking about
the other day. It uses PHP. If you share this script make sure to sanitise data.

<html>
<head>
</head>
<body>
<h1> Convert numbers </h1>
Use this form to convert numbers between different bases. <br/>
This can be useful for visualizating numbers in different bases. <br/>

<form>
Input base number:<br/>
<input type=”text” name=”base_in” value=”10″/><br/>
Numbers To Convert (1 per line)<br/>
<textarea name=”input_data” rows=”5″ cols=”20″>
<?php
if( isset( $_REQUEST["input_data"] ) )
{
echo $_REQUEST["input_data"];
}
?>
</textarea><br/>
Output base number:
<br/><input type=”text” name=”base_out” value=”2″/><br/>
<input type=”submit” name=”submit” value=”submit”/><br/>
</form>

<?php
if( isset( $_REQUEST["input_data"] ) )
{
$base_in = $_REQUEST["base_in"];
$base_out = $_REQUEST["base_out"];
$input_data = $_REQUEST["input_data"];
$lines = preg_split(“/(\r?\n)/”, $input_data);
$lines_count = count( $lines );

echo “<b> output in base $base_out</b><br/>”;
foreach( $lines as $line)
{
// do stuff with $line
echo base_convert( $line, $base_in, $base_out).”\n<br/>”;
}
}
?>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *