Anonymous user
Non-decimal radices/Convert: Difference between revisions
→{{header|Perl}}: Simple builtins, rewrite code, show modules.
(→{{header|PARI/GP}}: Loop was going wrong direction.) |
(→{{header|Perl}}: Simple builtins, rewrite code, show modules.) |
||
Line 1,743:
=={{header|Perl}}==
For base 2 and 16, we can do this entirely with language features:
<lang perl>
sub to16 { sprintf "%x", shift; }
sub from2 { unpack("N", pack("B32", substr("0" x 32 . shift, -32))); }
sub from16 { hex(shift); }</lang>
Small functions will handle arbitrary base conversions for bases 2-36:
<lang perl>sub base_to {
while ($n) {
}
}
sub base_from {
my($n,$b) = @_;
for my $c (split(//, lc($n))) {
$t = $b * $t + index("0123456789abcdefghijklmnopqrstuvwxyz", $c);
}
$t;
}</lang>
There are a plethora of modules that perform base conversion.
The core [https://metacpan.org/pod/distribution/perl/ext/POSIX/lib/POSIX.pod POSIX] module includes strtol (and strtoul) which is simple and fast, but only does conversions from a base. On some platforms the function may be limited to 32-bit even with a 64-bit Perl.
<lang perl>use POSIX;
my ($num, $n_unparsed) = strtol('1a', 16);
$n_unparsed == 0 or die "invalid characters found";
print "$num\n"; # prints "26"</lang>
The [https://metacpan.org/pod/ntheory ntheory] module includes functions that will perform base conversion, and is fast. It supports bases up to 36 and bigints.
<lang perl>
my $n = 65261;
my $n10 = fromdigits($n16, 16);
▲ $i < 10
say "$n $n16 $n10"; # prints "65261 feed 65261"</lang>
▲ : ('a' .. 'z')[$i - 10];}
Other modules include but are not limited to:
* [https://metacpan.org/pod/Math::BaseCalc Math::BaseCalc]
* [https://metacpan.org/pod/Math::Int2Base Math::Int2Base]
▲ {my ($int, $radix) = @_;
* [https://metacpan.org/pod/Math::NumberBase Math::NumberBase]
▲ my $numeral = '';
* [https://metacpan.org/pod/Convert::AnyBase Convert::AnyBase]
* [https://metacpan.org/pod/Math::BaseCnv Math::BaseCnv]
* [https://metacpan.org/pod/Math::BaseConvert Math::BaseConvert]
▲ } while $int = int($int / $radix);
▲ scalar reverse $numeral;}</lang>
The last two are ''much'' slower than the others or the simple functions above, but may have extra features. Math::Base::Convert and Convert::BaseN are currently not recommended.
The module [https://metacpan.org/pod/Math::Fleximal Math::Fleximal] not only does very arbitrary base conversion, but allows computations in different bases.
=={{header|Perl 6}}==
|