#!/usr/bin/perl
# version 0.5
# strings with ascii characters not exceeding 0x7f can be shortened by compression (use of free bits)
# the following was used as a guiding example: http://www.dreamfabric.com/sms/hello.html 
print "Enter 1 for string (septet) to hex octet\n";
print "Enter 2 for hex octet to string (septet)\n";
$selection = <STDIN>;
chop $selection;
if ( $selection == "1" ) {
   print "Enter the text string\n";
   $a = <STDIN>;
   chop $a;
   print "line entered is: $a\n";
   @out = unpack("C*",$a); # Unpack to binary
   $length_of_array_septet = scalar(@out);
   $length_of_array_octet = $length_of_array_septet;
   $index = 0;
   while ($index < $length_of_array_octet) {
      for ($byte=0,$shift=7;$byte < 7 && (($byte + $index) < $length_of_array_octet) ; $byte++,$shift--) {
          $counter = $byte + $index;
          $out[$counter]=$out[$counter] >> $byte | ((0xff >> $shift) & $out[$counter + 1]) << $shift;
      }
      for ($i=$index + 7;$i < $length_of_array_octet;$i++) {
         $out[$i] = $out[$i + 1];
      }
      $index = $index + 7;
      $length_of_array_octet = $length_of_array_octet - 1;
   }
   for ($index=0;$index <= $length_of_array_octet;$index++) {
      printf("%x ",$out[$index]);
   }
}
else {
   print "Enter the hex string (space separated)\n";
   $a = <STDIN>;
   chop $a;
   @out = unpack("C*",$a); # Unpack to binary
   $length_of_array_octet = scalar(@out);
   $counter = 0;
   $length_concatenation = $length_of_array_octet;
   for ($i=0;$i <= $length_of_array_octet;$i++) {
      if ($out[($i + 1)] == 32) {
         if (($out[$i] >= 48) && ($out[$i] <= 57)) { $out[$i] = $out[$i] - 48};
         if (($out[$i] >= 97) && ($out[$i] <= 102)) { $out[$i] = $out[$i] - 87};
         for ($j = $i ;$j < $length_concatenation;$j++) {
            $out[($j + 1)] = $out[($j + 2)];
         }
         $length_concatenation = $length_concatenation - 1;
      }
      else {
         if (($out[($i + 1)] >= 48) && ($out[($i + 1)] <= 57)) { $out[($i + 1)] = $out[($i + 1)] - 48};
         if (($out[($i + 1)] >= 97) && ($out[($i + 1)] <= 102)) { $out[($i + 1)] = $out[($i + 1)] - 87};
         if (($out[$i] >= 48) && ($out[$i] <= 57)) { $out[$i] = ($out[$i] - 48) * 16};
         if (($out[$i] >= 97) && ($out[$i] <= 102)) { $out[$i] = ($out[$i] - 87) * 16};
         $out[$i] = $out[($i + 1)] + $out[$i];
         for ($j = $i ;$j < $length_concatenation;$j++) {
            $out[($j + 1)] = $out[($j + 2)];
         }
         $length_concatenation = $length_concatenation - 1;
         for ($j = $i ;$j < $length_concatenation;$j++) {
            $out[($j + 1)] = $out[($j + 2)];
         }
         $length_concatenation = $length_concatenation - 1;
      } 
   }
   print "\n";
   print "Text is:\n";
   $index = 0;
   $index2 = 0;
   while (($length_of_array_octet - $index) > 8) {
      for ($i = 7, $j = 1;$i > 0 ; $i--,$j++) {
         $counter = $i + $index;
         $counter2 = $i + $index2;
         $out2[$counter2] = ($out[$counter] & (0x7f >> $i)) << $i | ($out[($counter - 1)] >> $j);
      }
      $out2[$index2] = $out[$index] & 0x7f;
      $index = $index + 7;
      $index2 = $index2 + 8;
   }
   for ($index=0;$index <= $length_of_array_octet;$index++) {
      printf("%c",$out2[$index]);
   }   
}

