[英]How to convert hex to char string in perl
I need change the %xx HEX characters to chars. 我需要将%xx十六进制字符更改为chars。 I am trying with this code but it does not works: 我正在尝试使用此代码,但它不起作用:
#!/usr/bin/perl -w
my $cadena = "%40%61%62";
print $cadena."\n";
$cadena =~ s/%//g;
print "cad: ".$cadena."\n";
my $string =~ s/([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;
print "traducida: ".$string;
Change 更改
my $string =~ s/([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;
to 至
$cadena =~ s/([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;
so that search and replace is done on $cadena
. 这样就可以在$cadena
上进行搜索和替换。
Output: @ab
输出: @ab
40 => @
61 => a
62 => b
A better regex pattern: 更好的正则表达式模式:
$cadena =~ s/([[:xdigit:]]{2})/chr(hex($1))/eg;
Use the POSIX character set [:xdigit:]
to match a single hexadecimal character and use {2}
to specify two and only two of them. 使用POSIX字符集[:xdigit:]
匹配单个十六进制字符,并使用{2}
指定两个,并且仅指定两个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.