Data::Translate - Translate string data between a few patterns (binary,decimal,ascii,hex)


Data_Translate documentation Contained in the Data_Translate distribution.

Index


Code Index:

NAME

Top

  Data::Translate - Translate string data between a few patterns (binary,decimal,ascii,hex)

SYNOPSIS

Top

  use Data::Translate;
  $data=new Translate;

  # Example, translating from hex to Ascii
  # $s receives the status of the operation

  @hh=qw(64 65 6e 61 6f);
  ($s,@ha)=$data->h2a(@hh);
  print join('',@ha),"\n"; ## will output "denao"

DESCRIPTION

Top

 This module is intended to translate data between a few patterns.
 Basicly, it is a ease mode to pack/unpack stuff.

 Imagine, you have a script that treats hex data, and you 
 need to see the values, in other format, like decimal, 
 binary or even ascii.

 This module implements a symplistic way to Translate values 
 smoothly returning the status of operation and values always 
 on a string.

 You may translate at this point:
   - ascii to binary
   - ascii to decimal
   - ascii to hex
   - decimal to ascii
   - decimal to binary
   - decimal to hex
   - binary to ascii
   - binary to decimal
   - binary to hex
   - hex to binary
   - hex to decimal
   - hex to ascii

 Please, head to test.pl for additional examples.
 The functions you'll call, are defined as the first
 byte of each data type.
 If you want to translate from binary to hex, you'll
 use the function b2h, and if you want to translate 
 from ascii to decimal, you'll use a2d, and so on.

->new
 Creates a new instance of Translation. 

 eg.: $data=new Translate;

->a2b Translate from ascii to binary Ex.:
   ($s,$bin)=$data->a2b($str);

->a2d Translate from ascii to decimal Ex.:
   ($s,@dec)=$data->a2d($str);

->a2h Translate from ascii to hexadecimal Ex.:
   ($s,$hh)=$data->a2h($str);

->b2a Translate from binary to ascii Ex.:
   ($s,$asc)=$data->b2a($bin);

->b2d Translate from binary to decimal Ex.:
   @t=unpack("A8" x (length($bin)/8),$bin);
   foreach $binary (@t) {
      ($s,$d)=$data->b2d($binary);
      print "--> $d\n";
   }

->b2h Translate from binary to hexadecimal Ex.:
   foreach $binary (@t) {
      ($s,$hx)=$data->b2h($binary);
      print "--> $hx\n";
   }

->d2a Translate from decimal to ascii Ex.: ($s,@a)=$data->d2a(@dec);
->d2b Translate from decimal to binary Ex.: ($s,@b)=$data->d2b(@dec);
->d2h Translate from decimal to hexadecimal Ex.: ($s,@bh)=$data->d2h(@dec);
->h2a Translate from hexadecimal to ascii Ex.: ($s,@ha)=$data->h2a(@hh);
->h2b Translate from hexadecimal to binary Ex.:
   ($s,@hd)=$data->h2d(@hh);

->h2d Translate from hexadecimal to decimal Ex.:
   ($s,@hb)=$data->h2b(@hh);

INSTALLATION perl Makefile.PL # build the Makefile make # build the package make test # test package make install # Install package =over

Top

AUTHOR Denis Almeida Vieira Junior <davieira@uol.com.br>.

Top

SEE ALSO

Top

 perl(1)


Data_Translate documentation Contained in the Data_Translate distribution.

package Data::Translate;

use vars qw($VERSION);
$VERSION = '0.2';

require Exporter;
@ISA       = qw(Exporter);
@EXPORT    = qw(a2b a2d a2h b2a b2d b2h d2a d2b d2h h2a h2b h2d new);
@EXPORT_OK = qw(a2b a2d a2h b2a b2d b2h d2a d2b d2h h2a h2b h2d new);

sub new {
    my    $obj = {};
    bless $obj;
    return $obj;
}

sub a2b {
  shift;
  local ($str)=@_;
  my $ss=unpack("B*",$str);
  return 1,$ss;
}

sub a2d {
  shift;
  local ($str)=@_;
  my @c=unpack("C" x length($str),$str);
  return 1,@c;
}

sub a2h {
  shift;
  local ($str)=@_;
  my @h=unpack("H2" x length($str), pack("A*",$str));
  return 1,@h;
}

sub b2a {
  shift;
  local ($binstr)= @_;
  if ($binstr=~/^[01]+$/) {
      $as=unpack("A*", pack("B*", $binstr));
      return 1,$as;
  } else {
      return -1,0;
  }
}

sub b2d {
  shift;
  local($v)=@_;my $a=$b=0;
  $a=unpack("N", pack( "B32", "0" x 24 . $v));
  return 1,$a;
}

sub b2h {
  shift;
  local($v)=@_;
  my $r=unpack("H8", pack("B8", $v));
  return 1,$r;
}

sub d2b {
  shift;
  local (@dec)=@_;
  for ($i=0;$i<=$#dec;$i++) {
     $dec[$i]=unpack("B*",pack("N",$dec[$i]));
     $dec[$i]=~s/^0+(?=\d{8})//;
  }
  return 1,@dec;
}

sub d2a {
  shift;
  local (@dec)=@_;
  for ($i=0;$i<=$#dec;$i++) {
    $dec[$i]=unpack("A*", pack("N", $dec[$i]));
  }
  return 1,@dec;
}

sub d2h {
  shift;
  local $t=join("",@_);
  local $tt=sprintf("%lx", $t);
  return 1,$tt;
}


#HEX
sub h2b {
  shift;
  local (@hex)=@_;my $i;
  for ($i=0;$i<=$#hex;$i++) {
      $hex[$i]=unpack("B8", pack("H*", $hex[$i]));
  }
  return 1,@hex;
}

sub h2d {
  shift;
  local (@hex)=@_;my $i;
  for ($i=0;$i<=$#hex;$i++) {
    $hex[$i]=ord(unpack("A",  pack("H*", $hex[$i])));
  }
  return 1,@hex;
}

sub h2a {
  shift;
  local (@hex)=@_;my $i;
  for ($i=0;$i<=$#hex;$i++) {
    $hex[$i]=unpack("A",pack("H8",$hex[$i]));
  }
  return 1,@hex;
}
1;
__END__