Location: PHPKode > scripts > binary-to-text (and back) converter > binary-to-text-and-back-converter/binary.php
<?php

 /***********************************************************************
 * binary.php - v 1.21 - 2001/10/14
 *
 * Snarkles.net Text-to-Binary Converter
 * =====================================
 * Send and decode messages in binary!
 *
 * This was my first script, so please be kind. :) Also, please forward
 * any suggestions, problems, improvements, etc. to the e-mail address
 * below. Thanks! :D
 *
 * Copyright (c) 2000, 2001 snarkles (hide@address.com)
 * Distributed under the GNU/GPL license (see http://www.gnu.org/copyleft/)
 ************************************************************************/
 /************************************************************************
 * Changelog
 * =========
 * v 1.22 - 2002/02/17:
 * - Cleaned up code, comments, HTML, etc.
 *
 * v 1.21 - 2001/10/14:
 * - Removed the "str_pad" function from the "txt2bin" portion for backwards
 *   compatibility with PHP3. Thanks, cyber junkie for the heads up!
 * - Added changelog and versioning system, since this probably isn't the
 *   first bug I'll have to squash. ;)
 *
 * v 1.2 - 2001/10/09:
 * - Added quick fix for Opera compatibility under txt2bin. Thanks for
 *   pointing out the problem, tommyj!
 * - Released the script *gulp!* :)
 *************************************************************************/
  $version = "1.22";

  // You can alter the HTML below to make this script fit more inline with 
  // the rest of your site.

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
 <head>
  <title> Snarkles.Net Text-to-Binary (and back!) Converter </title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  <style type="text/css">
  <!--
    body { font-family: "arial", "helvetica", sans-serif; font-size: 10pt; }
  -->
  </style>
 </head>
 <body>

<?php

  // Declare functions

    function txt2bin($str) {
      $text_array = explode("\r\n", chunk_split($str, 1));
      for ($n = 0; $n < count($text_array) - 1; $n++) {
        $newstring .= substr("0000".base_convert(ord($text_array[$n]), 10, 2), -8);
      }
      return $newstring;
    }

  function bin2txt($str) {
    $text_array = explode("\r\n", chunk_split($str, 8));
    for ($n = 0; $n < count($text_array) - 1; $n++) {
      $newstring .= chr(base_convert($text_array[$n], 2, 10));
    }
    return $newstring;
  }

  // Check to see if form was submitted yet

  if (isset($submit)) {

    // Yes, so make sure they filled something in

    if($text == '') {
      die("<p>Fill in the form, dinglefritz! ;)</p>\n");
    }

    // Looks good, so clean up data

    $text = stripslashes(urldecode(strip_tags($text)));

    // Make copy of original text for later display

    $orig_text = $text;

    // Time to process the form

    if ($convert == "txt2bin") {

      // Convert from text to binary

      $text = chunk_split(txt2bin($text), 8, " ");

    } elseif ($convert == "bin2txt") {

      // Convert from binary to text

      $text = bin2txt(str_replace("\r\n", "", str_replace(" ", "", $text)));

    } else {

      // This shouldn't happen

      die("<p>Hmmm. Now THAT'S no good. How about go back and try again?</p>\n");

    }

    // Display result

    echo("<p>$orig_text converts to:</p>\n");
    echo("<p>$text</p>\n");

} else {

  // Form has not been submitted, so display greeting

?>

  <center>
  <p>Welcome to the Snarkles.Net Text to Binary (and back!) Converter!</p>
  </center>

<?php

} // End big if

?>

  <!-- begin form -->

  <center>
  <p>Please insert text below:</p>

  <form method="post" action="<?php echo($PHP_SELF); ?>">
    <textarea name="text" rows="10" cols="45"></textarea><br />
    <input type="radio" name="convert" value="txt2bin" checked="checked" /> Convert from text to binary<br />
    <input type="radio" name="convert" value="bin2txt" /> Convert from binary to text<br />
    <input type="submit" name="submit" value="Convert!" />
    <input type="reset" value="Clear" />
  </form>
  </center>

  <!-- begin footer; it would be nice if you would leave this on. ;) -->

  <center>
   <p>
      <font size="1">Fine Print Shtuff:<br />
      Snarkles.net Text-to-Binary Converter - Version <?php echo($version); ?><br />
      &copy; 2000, 2001 <a href="http://snarkles.net">snarkles</a></font>
   </p>
  </center>

 </body>
</html>
Return current item: binary-to-text (and back) converter