View Single Post
Old Sep 3rd, 2007, 1:30 PM   #1
harryc
Newbie
 
Join Date: Sep 2007
Posts: 8
Rep Power: 0 harryc is on a distinguished road
anyone can help me in this php problem?

i need to convert double or floating point to binary but this program only accepts integer value such as 2,3,10 and so on but doesnt accept 2.5,3.45,10.0023 and so on..can anyone help me in this so that it can accept all kind of numbers..thanks

here is the coding

<?php

?>
<html>
<head>
  <title>Binary Converter</title>
  <style type="text/css">
  <!--
    P.error { margin-top: 0pt; color: red; font-weight: bold; }
    TR.grey { background: #eeeeee; }
  -->
  </style>
</head>
<body>
<div align="center">
<h1>Decimal to Binary & Binary to Decimal Converter</h1>

<?php
// look for no POST entries, or the RESET button
if (count($_POST) == 0 or isset($_POST['reset'])) {
    // POST array is empty - set initial values
    $dec_input   = null;
    $dec_output  = null;
    $base2value  = null;
   
} else {
    // retrieve values from POST array
    $dec_input   = &$_POST['dec_input'];
    $dec_output  = null;
    $base2value  = &$_POST['base2value'];
   
} // if

// initialise array for validation errors
$error = array();

if (isset($_POST['dec-2-bin'])) {
    $base2value = decbin($dec_input);
} // if

if (isset($_POST['bin-2-dec'])) {
    $dec_output = bindec($base2value);
    if ($error) {
       $error['base2value'] = $error[0];
    } // if
} // if

?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<table border="0">
<colgroup align="right">
<colgroup align="left">
<colgroup align="left">
<colgroup align="left">
<tr>
    <td>Decimal (input)</td>
    <td><input type="text" name="dec_input" value="<?php echo $dec_input ?>" />
<?php
    if (array_key_exists('dec_input', $error)) {
        echo '<p class="error">' .$error['dec_input'] .'</p>';
    } // if
?>    </td>
    <td colspan="2">&nbsp;</td>
    </tr>
<tr>
    <td>Decimal (output)</td><td colspan="3"><?php echo $dec_output ?></td>
</tr>
<tr class="grey">
    <td>Binary (Base 2)</td>
    <td><input type="text" name="base2value" value="<?php echo $base2value ?>" size="25" />
<?php
    if (array_key_exists('base2value', $error)) {
        echo '<p class="error">' .$error['base2value'] .'</p>';
    } // if
?>    </td>
    <td><input type="submit" name="dec-2-bin" value="DEC to BIN" /></td>
    <td><input type="submit" name="bin-2-dec" value="BIN to DEC" /></td>
</tr>
</table>
<p><input type="submit" name="reset" value="reset" /></p>
</form>

</div>
</body>
</html>
<?php

Last edited by big_k105; Sep 3rd, 2007 at 5:48 PM.
harryc is offline   Reply With Quote