Convert-Base81
view release on metacpan or search on metacpan
lib/Convert/Base81.pm view on Meta::CPAN
=head1 NAME
Convert::Base81 - Encoding and decoding to and from Base 81 strings
=head1 SYNOPSIS
use Convert::Base81;
my $encoded = Convert::Base81::encode($data);
my $decoded = Convert::Base81::decode($encoded);
or
use Convert::Base81 qw(base81_encode base81_decode);
my $encoded = base81_encode($data);
my $decoded = base81_decode($encoded);
=head1 DESCRIPTION
This module implements a I<Base81> conversion for encoding binary
data as text. This is done by interpreting each group of fifteen bytes
as a 120-bit integer, which is then converted to a seventeen-digit base 81
representation using the alphanumeric characters 0-9, A-Z, and a-z, in
addition to the punctuation characters !, #, $, %, (, ), *,
+, -, ;, =, ?, @, ^, _, {, |, }, and ~, in that order, characters that
are safe to use in JSON and XML formats.
lib/Convert/Base81.pm view on Meta::CPAN
=head3 base81_decode
=head3 Convert::Base81::decode
Converts the Base81-encoded string back to bytes. Any spaces, linebreaks, or
other whitespace are stripped from the string before decoding.
This function may be exported as C<base81_decode> into the caller's namespace.
If your original data wasn't an even multiple of fifteen in length, the
decoded data will have some padding with null bytes ('\0'), which can be removed.
#
# Decode the string and compare its length with the length of the original data.
#
my $decoded = base81_decode($data);
my $padding = length($decoded) - $datalen;
chop $decoded while ($padding-- > 0);
=cut
sub decode
{
my($encoded) = @_;
my($readsize, $writesize) = rwsize();
my $imul = uint128(81);
my $rem = uint128();
t/01-encode64.t view on Meta::CPAN
for my $pair (@codings)
{
my ($text, $encoded) = @$pair;
my $l = length($text);
my $test_encode = base81_encode($text);
ok($test_encode eq $encoded, "${tno}a: '$text' encoded into '$test_encode', not '$encoded'");
my $test_decode = base81_decode($encoded);
my $padding = length($test_decode) - $l;
#diag("Difference in lengths between original and decoded is ", $padding);
chop $test_decode while ($padding-- > 0);
ok($test_decode eq $text, "${tno}b: '$encoded' decoded into '$test_decode', not '$text'");
$tno += 1;
}
t/02-encode128.t view on Meta::CPAN
for my $pair (@codings)
{
my ($text, $encoded) = @$pair;
my $l = length($text);
my $test_encode = base81_encode($text);
ok($test_encode eq $encoded, "${tno}a: '$text' encoded into '$test_encode', not '$encoded'");
my $test_decode = base81_decode($encoded);
my $padding = length($test_decode) - $l;
#diag("Difference in lengths between original and decoded is ", $padding);
chop $test_decode while ($padding-- > 0);
ok($test_decode eq $text, "${tno}b: '$encoded' decoded into '$test_decode', not '$text'");
$tno += 1;
}
( run in 0.344 second using v1.01-cache-2.11-cpan-26ccb49234f )