且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编码西里尔字符的URL,然后解码?

更新时间:2023-02-23 12:01:38

正确的解决方案,包括空格:

Correct solution, including spaces:

use open ':std', ':encoding(UTF-8)';
use Encode;

my $escaped = '%41F%2F%424+%41F%41E%414%416%410%420%41A%410+%418%417+%421%412%418%41D';
(my $unescaped = $escaped) =~ s/\+/ /g;
$unescaped =~ s/%([[:xdigit:]]+)/chr hex $1/eg;
print $unescaped;
# П/Ф ПОДЖАРКА ИЗ СВИН

Credit转到 Renaud Bompuis 首先识别这些是以为前缀的Unicode代码点。

Credit goes to Renaud Bompuis for recognising as the first that these are Unicode code-points prefixed with %.

我想补充一点,这个问题的编码方案非常不寻常,我以前没有见过。通常,人们期望字符串П/ФПОДЖАРКАИЗСВИН被编码为%D0%9F%2F%D0%A4 +%D0%9F %D0%9E%D0%94%D0%96%D0%90%D0%A0%D0%9A%D0%90 +%D0%98%D0%97 +%D0%A1%D0%92%D0%98 %D0%9D ,也就是说,首先将字符编码为UTF-8,然后八位字节进行百分比转义。此方案适用于 Dr.Kameleon 的答案。

I wish to add that the encoding scheme from the question is very unusual, I haven't seen it before. Normally one would expect the characters string П/Ф ПОДЖАРКА ИЗ СВИН to be encoded as %D0%9F%2F%D0%A4+%D0%9F%D0%9E%D0%94%D0%96%D0%90%D0%A0%D0%9A%D0%90+%D0%98%D0%97+%D0%A1%D0%92%D0%98%D0%9D, that is to say, first the characters are encoded into UTF-8, then the octets are percent-escaped. This scheme works with the answer from Dr.Kameleon.