Brad Baxter > Math-Int2Base > Math::Int2Base

Download:
Math-Int2Base-1.00.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  1
View/Report Bugs
Module Version: 1.00   Source  

NAME ^

Math::Int2Base - Perl extension for converting decimal (base-10) integers into another number base from base-2 to base-62, and back to decimal.

SYNOPSIS ^

  use Math::Int2Base qw( int2base base2int base_chars );

  my $base = 16;  # i.e., hexidecimal
  my $hex = int2base( 255,  $base );  # FF
  my $dec = base2int( $hex, $base );  # 255

  my $sixdig = int2base( 100, 36, 6  ); # 00002S
  my $decno  = base2int( $sixdig, 36 ); # 100, i.e., leading zeros no problem

  my $chars = base_chars( 24 );  # 0123...KLMN
  my $regex = qr/^[$chars]$/;    # used as character class

  use bigint;  # if needed
  my $googol = 10**100;
  my $base62 = int2base( $googol, 62 );  # QCyvrY2MJnQFGlUHTCA95Xz8AHOrLuoIO0fuPkHHCcyXy9ytM5N1lqsa
  my $bigint = base2int( $base62, 62 );  # back to 1e+100

  # from one base to another (via base-10)
  sub base_convert {
      my( $num, $from_base, $to_base ) = @_;
      return int2base( base2int( $num, $from_base ), $to_base );
  }

DESCRIPTION ^

Math::Int2Base provides

This module only works with positive integers. Fractions are silently truncated to integers.

CONSTRAINTS ^

SEE ALSO ^

http://en.wikipedia.org/wiki/Category:Positional_numeral_systems Math::BaseCnv Math::BaseCalc

The code is based on newsgroup discussion http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/3f3b416e3a79fd2/d2f62e10c837e782 particularly that of Dr. Ruud. Errors are my own.

AUTHOR ^

Brad Baxter, <bbaxter@cpan.org>

COPYRIGHT AND LICENSE ^

Copyright (C) 2010 by Brad Baxter

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: