Data::Translate - Translate string data between a few patterns (binary,decimal,ascii,hex)
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"
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);
perl(1)