Convert-BaseN

 view release on metacpan or  search on metacpan

lib/Convert/BaseN.pm  view on Meta::CPAN

    $bn = __PACKAGE__->new(base => 16, chars => '0-9A-F');
    $encoded = $bn->encode("dankogai", " ");
    warn $encoded;

    $bn = __PACKAGE__->new(base => 32);
    $encoded = $bn->encode("dankogai");
    warn $encoded;
    warn $bn->decode($encoded);
    warn length $bn->decode($encoded);

    $bn = __PACKAGE__->new(base => 32, chars => 'A-Z2-7=');
    $encoded = $bn->encode("dankogai");
    warn $encoded;
    warn $bn->decode($encoded);
    warn length $bn->decode($encoded);

    $bn = __PACKAGE__->new(base => 64);
    $encoded = $bn->encode("dankogai");
    warn $encoded;
    warn $bn->decode($encoded);

    $bn = __PACKAGE__->new(base => 64,chars => '0-9A-Za-z\-_=');
    $encoded = $bn->encode(join("", map {chr} 0x21 .. 0x7e), "\n", 40);
    warn $encoded;
    warn $bn->decode($encoded);
    warn scalar unpack "H*", $bn->decode('-__-');

    $bn = __PACKAGE__->new('base69');
    #warn $bn->encode("dankogai");
    #$bn = __PACKAGE__->new(name => 'base4');
    #$bn = __PACKAGE__->new(name => 'basex');
    #$bn = __PACKAGE__->new(base => 17);
}

1;    # End of Convert::BaseN

=head1 NAME

Convert::BaseN - encoding and decoding of base{2,4,8,16,32,64} strings

=head1 VERSION

$Id: BaseN.pm,v 0.1 2008/06/16 17:34:27 dankogai Exp dankogai $

=cut

=head1 SYNOPSIS

  use Convert::BaseN;
  # by name
  my $cb = Convert::BaseN->new('base64');
  my $cb = Convert::BaseN->new( name => 'base64' );
  # or base
  my $cb = Convert::BaseN->new( base => 64 );
  my $cb_url = Convert::BaseN->new(
    base  => 64,
    chars => '0-9A-Za-z\-_=' 
  );
  # encode and decode
  $encoded = $cb->encode($data);
  $decoded = $cb->decode($encoded);

=head1 EXPORT

Nothing.  Instead of that, this module builds I<transcoder object> for
you and you use its C<decode> and C<encode> methods to get the job
done.

=head1 FUNCTIONS

=head2 new

Create the transcoder object.

  # by name
  my $cb = Convert::BaseN->new('base64');
  my $cb = Convert::BaseN->new( name => 'base64' );
  # or base
  my $cb = Convert::BaseN->new( base => 64 );
  my $cb_url = Convert::BaseN->new(
    base  => 64,
    chars => '0-9A-Za-z\-_=' 
  );

You can pick the decoder by name or create your own by specifying base
and character map.

=over 2

=item base

Must be 2, 4, 16, 32 or 64.

=item chars

Specifiles the character map.  The format is the same as C<tr>.

  # DNA is coded that way.
  my $dna = Convert::BaseN->new( base => 4, chars => 'ACGT' );

=item padding

=item nopadding

Specifies if padding (adding '=' or other chars) is required when
encoding.  default is yes.

  # url-safe Base64
  my $b64url = Convert::BaseN->new( 
    base => 64, chars => '0-9A-Za-z\-_=', padding => 0;
  );

=item name

When specified, the following pre-defined encodings will be used.

=over 2

=item base2

base 2 encoding. C<perl> is C<01110000011001010111001001101100>.

=item base4

=item DNA

=item RNA

base 4 encodings. C<perl> is:

  base4: 1300121113021230
  DNA:   CTAACGCCCTAGCGTA
  RNA:   GAUUGCGGGAUCGCAU

base 16 encoding. C<perl> is C<7065726c>.

=item base32

=item base32hex

base 32 encoding mentioned in RFC4648.  C<perl> is:

  base32:    OBSXE3A==
  base32hex: E1IN4R0==

=item base64

=item base64_url

=item base64_imap

=item base64_ircu

base 64 encoding, as in L<MIME::Base64>.  They differ only in
characters to represent number 62 and 63 as follows.

  base64:        +/
  base64_url:    -_
  base64_imap:   +,
  base64_ircu:   []

for all predefined base 64 variants, C<decode> accept ANY form of those.

=back

=back

=head2 decode

Does decode

  my $decoded = $cb->decode($data)

=head2 encode

Does encode.

  # line folds every 76 octets, like MIME::Base64::encode
  my $encoded = $cb->encode($data);
  # no line folding (compatibile w/ MIME::Base64)
  my $encoded = $cb->encode($data, "");
  # line folding by CRLF, every 40 octets
  my $encoded = $cb->encode($data, "\r\n", 40);

=head1 SEE ALSO

RFC4648 L<http://tools.ietf.org/html/rfc4648>

Wikipedia L<http://en.wikipedia.org/wiki/Base64>

L<http://www.centricorp.com/papers/base64.htm>

L<MIME::Base64>

L<MIME::Base32>

L<MIME::Base64::URLSafe>

=head1 AUTHOR

Dan Kogai, C<< <dankogai at dan.co.jp> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-convert-basen at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Convert-BaseN>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Convert::BaseN

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Convert-BaseN>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Convert-BaseN>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Convert-BaseN>

=item * Search CPAN



( run in 4.374 seconds using v1.01-cache-2.11-cpan-2398b32b56e )