如何将Perl
中的数字编码为8个字母的字母数字字符串,以字母开头,以结尾数位为校验位的数字结尾。
那么如何生成校验位并且我计划从21767823360
开始计数器,以便我的结果字符串以A000000
开头,但perl没有用这么大的数字进行计算。
请提出解决方案。
$AppID=alphanumero($appid,8,1);
sub alphanumero{
my ($n,$length,$type)=@_;
my @to_b36 = (0 .. 9, 'A' .. 'Z');
use integer; # so that /= 36 is easy
my $u=$n%10;$n=21767823360+($n-$u)/10;
my $t = "";do { $t = $to_b36[$n % 36] . $t, $n /= 36 } while $n;
return "$t$u";
}
答案 0 :(得分:4)
Perl在大数字方面几乎没有问题,如果你的数字真的很大,只需要use bignum
。这可以透明地实现无限精确的算术。
您的电话号码21767823360需要大约35位。我的perl
是使用64位整数构建的(请参阅perl -v
以查看您的支持),因此您的号码对我来说并不“太大”。
将数字转换为base-n的算法很简单:
# pseudocode
let "digits" be the array containing all the digits of our representation.
# the size of digits is the base of our new representation
# the digits are sorted in ascending order.
#digits[0] is zero.
var "n" is the number we want to represent.
var "size" is the number of digits of the new representation.
# floor(ln(n)/ln(digits.size))
var "representation" is the empty string.
while size >= 0:
representation ← representation.concat(digits[n / digits.length ^ size]).
n ← n.modulo(digits.length ^ size).
size ← size - 1.
return representation.
示例Perl:
#!/usr/bin/perl
use strict; use warnings;
use Carp;
sub base_n {
my ($number, $base, $max_digits, $pad) = @_;
defined $number or croak "Undefined number for base_n";
$number == int $number and $number >= 0
or croak "The number has to be a natural number for base_n";
defined $base or croak "Undefined base for base_n";
$base == int $base and $base > 0
or croak "The base has to be a positive integer for base_n";
my @digits = (0 .. 9, "A" .. "Z");
$base <= @digits or croak "base_n can only convert to base-" . @digits . " max.";
@digits = @digits[0 .. $base - 1];
my $size = $number ? int(log($number) / log($base)) : 0; # avoid log(0)
if (defined $max_digits) {
$size < $max_digits
or croak "The number $number is too large for $max_digits digits in base $base.";
$size = $max_digits - 1 if $pad;
}
my $representation = "";
while ($size >= 0) {
$representation .= $digits[$number / @digits**$size];
$number %= @digits**$size;
$size--;
}
if (wantarray) {
my $checksum = substr $representation, -1;
return $representation, $checksum;
} else {
return $representation;
}
}
相应(但不完整)的单元测试:
use Test::More;
my $n = 21767823360;
ok "A000000" eq base_n($n => 36), "simple";
ok "A000000" eq base_n($n => 36, 8), "valid constraint";
ok "0A000000" eq base_n($n => 36, 8, 1), "padding";
ok ! eval { base_n($n => 36, 6); 1 }, "invalid constraint";
ok "0" eq (base_n($n => 36))[1], "checksum (1)";
ok "A" eq (base_n($n+10 => 36))[1], "checksum (2)";
ok "0" eq base_n(0 => 36), "zero: simple";
ok "0"x8 eq base_n(0 => 36, 8, 1), "zero: padding";
ok ! eval { base_n($n => 0.7); 1 }, "invalid base";
ok ! eval { base_n(0.7 => 36); 1 }, "invalid number";
ok $n == base_n($n => 10), "round-trip safety";
ok $n eq base_n($n => 10, length $n, 1), "round-trip safety: padding";
done_testing;