line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Int2Base; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22361
|
use 5.008006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
33
|
|
6
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
99
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
1
|
|
|
1
|
|
10
|
use base qw(Exporter); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
603
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw( int2base base2int base_chars ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my @Chars = ('0'..'9', 'A'..'Z', 'a'..'z'); |
16
|
|
|
|
|
|
|
my $MaxBase = scalar @Chars; |
17
|
|
|
|
|
|
|
my $CharStr = join '', @Chars; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
20
|
|
|
|
|
|
|
# int2base( $num, $base, $minlen ); # base ||= 10 minlen ||= 1 |
21
|
|
|
|
|
|
|
sub int2base { |
22
|
|
|
|
|
|
|
|
23
|
134
|
|
|
134
|
0
|
78888
|
my( $ret, $num, $base, $minlen ) = ( '', @_ ); |
24
|
134
|
|
100
|
|
|
493
|
$num ||= 0; |
25
|
134
|
|
50
|
|
|
425
|
$base ||= 10; |
26
|
134
|
|
100
|
|
|
553
|
$minlen ||= 1; |
27
|
|
|
|
|
|
|
|
28
|
134
|
100
|
100
|
|
|
1095
|
if( $num < 0 |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
29
|
|
|
|
|
|
|
|| $base < 2 |
30
|
|
|
|
|
|
|
|| $MaxBase < $base |
31
|
|
|
|
|
|
|
|| $minlen < 1 |
32
|
|
|
|
|
|
|
# || $num != int( $num ) # XXX[1] do we care? |
33
|
|
|
|
|
|
|
) { |
34
|
4
|
|
|
|
|
610
|
croak "not supported: int2base( '$num', $base, $minlen )" } |
35
|
|
|
|
|
|
|
|
36
|
130
|
|
|
|
|
1399
|
for (; $num; $num = int($num/$base) ) { $ret .= $Chars[$num % $base] } |
|
243
|
|
|
|
|
19782
|
|
37
|
130
|
|
|
|
|
1383
|
return scalar reverse $ret . '0'x($minlen - length($ret)); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
41
|
|
|
|
|
|
|
# base2int( $num, $base ); # base ||= 10 |
42
|
|
|
|
|
|
|
sub base2int { |
43
|
|
|
|
|
|
|
|
44
|
68
|
|
|
68
|
0
|
39634
|
my( $ret, $num, $base ) = ( 0, @_ ); |
45
|
68
|
|
100
|
|
|
181
|
$num ||= 0; |
46
|
68
|
|
50
|
|
|
139
|
$base ||= 10; |
47
|
68
|
|
|
|
|
194
|
my $chars = substr $CharStr, 0, $base; |
48
|
|
|
|
|
|
|
|
49
|
68
|
100
|
66
|
|
|
1073
|
if( $num !~ /^[$chars]+$/ |
|
|
|
100
|
|
|
|
|
50
|
|
|
|
|
|
|
|| $base < 2 |
51
|
|
|
|
|
|
|
|| $MaxBase < $base ) { |
52
|
4
|
|
|
|
|
608
|
croak "not supported: base2int( '$num', $base )" } |
53
|
|
|
|
|
|
|
|
54
|
64
|
|
|
|
|
596
|
$num =~ s/^0+//; # trim leading zeros |
55
|
64
|
|
|
|
|
201
|
for( my $i = length($num)-1, my $c = 0; $i >= 0; --$i ) { |
56
|
170
|
|
|
|
|
57951
|
$ret += index($CharStr, substr($num, $i, 1)) * $base**$c++ } |
57
|
64
|
|
|
|
|
1239
|
return $ret; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
61
|
|
|
|
|
|
|
# base_chars( $base ); # base ||= 10 |
62
|
|
|
|
|
|
|
sub base_chars { |
63
|
2
|
|
|
2
|
0
|
763
|
my( $base ) = @_; |
64
|
2
|
|
50
|
|
|
7
|
$base ||= 10; |
65
|
2
|
|
|
|
|
10
|
return substr $CharStr, 0, $base; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
__END__ |