Jaren

Basic Binary Encoder/Decoder

Dec 14th, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. <?php
  2. if($_GET['Action'] == 'Encode'){
  3. $array = str_split($_GET['Text']);
  4. $result = '';
  5. foreach($array as $item){
  6.     $bin = decbin(ord($item));
  7.     $bin = str_pad($bin, 8, "0", STR_PAD_LEFT);
  8.     $result .= $bin;
  9. }
  10. echo $result;
  11. } elseif($_GET['Action'] == 'Decode'){
  12.     $array = str_replace(' ','',$_GET['Text']);
  13.     if(strlen($array) % 8 !== 0){
  14.         header('Location: Binary.php?Error=1');
  15.         exit;
  16.     } else {
  17.         if(substr_count($_GET['Text'],' ') !== 0){
  18.             header('Location: /Binary.php?Text='.urlencode(str_replace(' ','',$_GET['Text'])).'&Action=Decode');
  19.             exit;
  20.         }
  21.     $array = str_split($array,8);
  22.     $bin = '';
  23.     foreach($array as $item){
  24.     $result .= chr(bindec($item));
  25.     }
  26.     echo $result;
  27.     }
  28. } elseif(isset($_GET['Text']) && strlen($_GET['Text']) !== 0) {
  29.     $value = str_replace(' ','',$_GET['Text']);
  30.     if(is_numeric($value) && substr_count($value,1)+substr_count($value,0) === strlen($value) && strlen($value) %8 === 0){
  31.         header('Location: /Binary.php?Text='.urlencode($value).'&Action=Decode');
  32.         exit;
  33.     } else {
  34.         header('Location: /Binary.php?Text='.urlencode($_GET['Text']).'&Action=Encode');
  35.         exit;
  36.     }
  37. } else {
  38. ?>
  39.  
  40. <form action="" method="get">
  41. <input type="text" name="Text"><br>
  42. <input type="radio" name="Action" value="Encode">Encode<br>
  43. <input type="radio" name="Action" value="Decode">Decode<br>
  44. <input type="submit" value="Submit">
  45. </form>
  46.  
  47. <?php
  48. }
Advertisement
Add Comment
Please, Sign In to add comment