kloop
kloop

Reputation: 4721

How to convert a number to an arbitary base in perl?

I would like to do something like the following in perl:

@digits = ("1", "2", ..., "a", ... "z", ... ); ## a list of characters
$num = 1033;
convert_to_base($num, @digits);

Now, $num will be converted to a string with the digits being used are from digits (so the base is $#digits + 1).

It can be done by iterating on $num, taking the modulo of $num with respect to $#digits and then dividing until 0 is reached, but I was wondering if there is any built-in function that does that in perl (or alternatively a fast function that would do it in perl).

Upvotes: 4

Views: 1756

Answers (2)

insaner
insaner

Reputation: 1705

As @Adam Katz mentioned in his answer, the way to do it is with Math::Base::Convert. However, your question is about using an arbitrary base. The CPAN pod isn't very clear on how to do it, but it's actually as easy as:

use strict;

use Math::Base::Convert;    # https://metacpan.org/pod/Math::Base::Convert

my $arb_enc = ['0'..'9', 'B'..'D', 'F'..'H', 'j'..'n', 'p'..'t', 'v'..'z', '*', '~'] ;
#  ^^^^ note this is a array ref, which you can build with whatever characters you want

my $d_arbenc = new Math::Base::Convert('10', $arb_enc); # from decimal
my $arbenc_d = new Math::Base::Convert( $arb_enc, '10'); # to decimal

# test it like this:
foreach ( "1", "123", 62, 64, 255, 65535, 100, 10000, 1000000 ) {
    my $status = eval {  $d_arbenc->cnv($_) }; # if error, $status will be empty, and error message will be in $@
    print "d_arbenc [$_] = [$status]\n";
    }

foreach ( "BD3F", "jjjnnnppp", "333", "bad string" ) {
    my $status = eval { $arbenc_d->cnv($_) }; # if error, $status will be empty, and error message will be in $@
    print "arbenc_d [$_] = [$status]\n";    
    }

Upvotes: 2

Adam Katz
Adam Katz

Reputation: 16138

Using Math::Base::Convert as suggested in choroba's comment to the question:

#!/usr/bin/perl
use Math::Base::Convert "cnv";

my $num = 1033;

printf "%b", $num;           # binary:      10000001001
printf "%o", $num;           # octal:       2011
printf "%d", $num;           # decimal:     1033
printf "%x", $num;           # hexadecimal: 409
print cnv($num, 10, b64);    # base64*:     G9   (*: 0-9, A-Z, a-z, ., _)
print cnv($num, 10, b85);    # base85*:     CD   (*: from RFC 1924)
print cnv($num, 10, ascii);  # base96:      *s

Note, if you need to interpret this as a string, you may have to do e.g.
printf "%s", "" . cnv($num, 10, b85);

Upvotes: 4

Related Questions