[英]How to increment hex in perl
CODE 码
use warnings;
use strict;
my $mv = 41;
my $tmp =1;
while($tmp<26)
{
print chr (hex($mv++));
print "\n";
$tmp++;
}
OUTPUT ABCDEFGHIPQRSTUVWXY`abcde 输出 ABCDEFGHIPQRSTUVWXY`abcde
Code generate English character set 代码生成英文字符集
Issue 问题
Few characters are missing "J->O && Z " 缺少几个字符“ J-> O && Z”
Reason J hex value is 4a 原因J十六进制值为4a
How to increment the hex value in perl or any another way to generate the character set? 如何在perl中以其他方式递增十六进制值或生成字符集的任何其他方式?
According to a comment you left, your end goal appears to be to produce the mapping A=1,B=2. 根据您留下的评论,您的最终目标似乎是产生映射A = 1,B = 2。 Here's code to achieve that:
这是实现此目的的代码:
my @symbols = 'A'..'Z';
my %map = map { $symbols[$_] => $_+1 } 0..$#symbols;
Or (less flexible): 或(不太灵活):
my %map = map { $_ => ord($_)-ord('A')+1 } 'A'..'Z';
You may want 你可能想要
for my $i (65..122) {
print chr($i);
}
Also you may like 你也可能喜欢
for my $char ("a".."z", "A".."Z") {
print $char;
}
Setting aside your actual stated goal (the mapping of characters to codes), the problem here is that $mv
is not a hexadecimal value, it's a decimal value that you stringify and treat as hexadecimal. 除了实际的既定目标(字符到代码的映射)之外,这里的问题是
$mv
不是十六进制值,而是将其字符串化并视为十六进制的十进制值。 That means that the next value after 49 is 50, not 4a. 这意味着49之后的下一个值为50,而不是4a。 If
$mv
were in hex from the outset, you wouldn't have this problem (and you wouldn't need the call to hex()
, either). 如果
$mv
从一开始就是十六进制的,就不会有这个问题(也不需要调用hex()
)。 If you declare $mv
as so: 如果这样声明
$mv
:
$mv = 0x41;
then you will find that the value 49 is correctly followed by 4a. 那么您会发现值49正确地跟着4a。 Using your code example:
使用您的代码示例:
my $mv = 0x41;
my $tmp = 1;
while ($tmp < 26)
{
print chr($mv++);
print "\n";
$tmp++;
}
You should get the original intended results. 您应该得到原始的预期结果。
尝试类似
print chr for (65..90);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.