如何生成具有不同基数的字符序列(例如十六进制)?
我有以下 Perl 脚本,它根据数字生成字符串:
my @chars;
push @chars, map(chr, 48..57), map(chr, 97..122);
my $c = $#chars+1;
for (0..50) {
my $string;
my $l = $_ / $c;
my $i = int $l;
my $r = ($l - $i) * $c;
$string .= $chars[$r];
while ($i > 0) {
$l = $i / $c;
$i = int $l;
$r = ($l - $i) * $c;
$string .= $chars[$r];
}
print "$string\n";
}
当我运行此脚本时,我得到以下输出:
0
1
2
3
4
...
z
01
01
21
21
41
41
61
61
81
91
91
b1
b1
d1
d1
我缺少什么? 感谢您的帮助!
I have the following Perl script that generates a string based on a number:
my @chars;
push @chars, map(chr, 48..57), map(chr, 97..122);
my $c = $#chars+1;
for (0..50) {
my $string;
my $l = $_ / $c;
my $i = int $l;
my $r = ($l - $i) * $c;
$string .= $chars[$r];
while ($i > 0) {
$l = $i / $c;
$i = int $l;
$r = ($l - $i) * $c;
$string .= $chars[$r];
}
print "$string\n";
}
When I run this I get the following output:
0
1
2
3
4
...
z
01
01
21
21
41
41
61
61
81
91
91
b1
b1
d1
d1
What am I missing? Thankful for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,它比您拥有的脚本更清晰一点,并且可以正确转换为任意基数:
Try this instead, it's a little clearer than the script you have and properly converts to an arbitrary base:
另请参阅http://www.rosettacode.org/wiki/Number_base_conversion#Perl
See also http://www.rosettacode.org/wiki/Number_base_conversion#Perl