Advertisement
jackwilder

Binary to Text

Oct 22nd, 2015
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.39 KB | None | 0 0
  1. #!/usr/bin/perl -W
  2.  
  3. use strict;
  4. my @in;
  5. my $file;
  6.  
  7. sub tobinary() {
  8.  my @in;
  9.  my $out;
  10.  my $x;
  11.  @in = @_;
  12.  for($x=0;$x<=$#in;$x++) {
  13.      $out .= unpack("B*", $in[$x]);
  14.  }
  15.  print("$out");
  16.  print("\n");
  17. }
  18.  
  19. sub totext() {
  20.  my @in;
  21.  my $out;
  22.  my $x;
  23.  @in = @_;
  24.  for($x=0;$x<=$#in;$x++) {
  25.      $out = pack("B*", $in[$x]);
  26.   print($out);
  27.  }
  28.  print("\n");
  29. }
  30.  
  31. sub help() {
  32.  print("\nUsage: $0 [-b | -t] [-f filename | \"Test to convert\"]\n\n");
  33.  print(" -b        : text to binary conversion\n");
  34.  print(" -t        : binary to text conversion\n");
  35.  print(" filename  : file to be converted\n");
  36.  print(" message   : text to be converted\n");
  37. }
  38.  
  39. if (@ARGV < 2) {
  40.  &help();
  41.  exit(1);
  42. }
  43.  
  44. if ($ARGV[0] eq '-b') {
  45.   if ($ARGV[1] eq '-f') {
  46.      $file = $::ARGV[2];
  47.      open(F, "<$file") || die "Error: unable to open $file - $!\n";
  48.  
  49.      @in = <F>;
  50.      close(F);
  51.      if (!@in) {
  52.         print("Error: no data in $file\n");
  53.         exit(1);
  54.      }
  55.   } else {
  56.      @in = $::ARGV[1];
  57.   }
  58.   &tobinary(@in);
  59. }
  60. elsif ($ARGV[0] eq '-t') {
  61.   if ($ARGV[1] eq '-f') {
  62.      $file = $::ARGV[2];
  63.      open(F, "<$file") || die "Error: unable to open $file - $!\n";
  64.  
  65.      @in = <F>;
  66.      close(F);
  67.      if (!@in) {
  68.         print("Error: no data in $file\n");
  69.         exit(1);
  70.      }
  71.   } else {
  72.      @in = $::ARGV[1];
  73.   }
  74.   &totext(@in);
  75. }
  76. else {
  77.  &help();
  78.  exit(1);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement