Guest User

Untitled

a guest
Apr 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. if ($_POST['post'] == "true")
  6.  
  7. {
  8.  
  9.  
  10.  
  11. $decimal = $_POST['dec_num'];
  12.  
  13. $decimal = round($decimal);
  14.  
  15. $divisor = 0;
  16.  
  17. $remainder = 0;
  18.  
  19. $remainders = array();
  20.  
  21. $iteration = 0;
  22.  
  23. $i = 0;
  24.  
  25.  
  26.  
  27. $next = $decimal;
  28.  
  29.  
  30.  
  31. if ($decimal > 256)
  32.  
  33. die("<b>ERROR</b>: Overflow");
  34.  
  35.  
  36.  
  37. if ($decimal < 0)
  38.  
  39. die("<b>ERROR</b>: Negative Number");
  40.  
  41.  
  42.  
  43. for(;;)
  44.  
  45. {
  46.  
  47.  
  48.  
  49. for(;;)
  50.  
  51. {
  52.  
  53.  
  54.  
  55. // highest divisor found, but it's too high, so go back a step
  56.  
  57. if ($divisor > $next)
  58.  
  59. {
  60.  
  61. $divisor /= 2;
  62.  
  63. $iteration--;
  64.  
  65. break;
  66.  
  67. }
  68.  
  69.  
  70.  
  71. $iteration++;
  72.  
  73. $divisor = 2 * $iteration;
  74.  
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81. $remainder = $next - (2 * $iteration);
  82.  
  83.  
  84.  
  85. // failsafe
  86.  
  87. if ($remainder > 1)
  88.  
  89. die("<b>ERROR</b>: Invalid Remainder");
  90.  
  91.  
  92.  
  93. $remainders[$i] = $remainder;
  94.  
  95.  
  96.  
  97. // cannot apply division algorithm anymore
  98.  
  99. if ($iteration == 0)
  100.  
  101. break;
  102.  
  103.  
  104.  
  105. $divisor = 1;
  106.  
  107. $i++;
  108.  
  109. $next = $iteration;
  110.  
  111. $iteration = 0;
  112.  
  113.  
  114.  
  115. }
  116.  
  117.  
  118.  
  119. $sizeof = count($remainders);
  120.  
  121. for ($k = 8; $k > $sizeof; $k--)
  122.  
  123. $bin_num .= 0;
  124.  
  125.  
  126.  
  127.  
  128.  
  129. for ($j = $sizeof; $j + 1 > 0; $j--)
  130.  
  131. $bin_num .= $remainders[$j];
  132.  
  133.  
  134.  
  135. /*
  136.  
  137. for ($j = 0; $j < $i + 1; $j++)
  138.  
  139. echo "remainders[$j]:" . $remainders[$j] . "<br>";
  140.  
  141. */
  142.  
  143. }
  144.  
  145.  
  146.  
  147. ?>
  148.  
  149. <h1>Decimal to Binary Converter</h1>
  150.  
  151. <h2>Unsigned, 8 bits</h2>
  152.  
  153. <h3>Integers 0 - 255</h3>
  154.  
  155. <form id="conv_form" name="conv_form" method="POST">
  156.  
  157. <b>Decimal Number</b>: <input type="text" name="dec_num" id="dec_num" maxlength="3" size="1" class="input"><br /><br />
  158.  
  159. <input type="hidden" name="post" value="true">
  160.  
  161. <input type="submit" value="Convert" id="conv_but" class="input">
  162.  
  163.  
  164.  
  165. <?php
  166.  
  167.  
  168.  
  169. if (isset($bin_num))
  170.  
  171. echo "<br /><br /><b>Your 8-bit Binary Number is</b>: " . $bin_num;
  172.  
  173.  
  174.  
  175. ?>
Add Comment
Please, Sign In to add comment