How can I use a Perl one-liner to decode an ASCII string encoded in hexadecimal?

微信扫一扫,分享到朋友圈

How can I use a Perl one-liner to decode an ASCII string encoded in hexadecimal?

I’d like to write a Perl one-liner to decode a line of ASCII characters encoded as hexadecimal numbers (for example the line 48 54 54 50
should be decoded as HTTP
). I came up with this:

perl -nE 'say map(chr, map { qq/0x$_/ } split)'

It prints an empty line. What am I doing wrong and how would you write it?

It’s your qq/0x$_/
trick that doesn’t work. chr
expects a number as argument, but gets the string literal "0x48"
. Use the hex
function to convert 48
to a decimal number, like datageist does in his answer.

This works for me:

echo '48 54 54 50' | perl -nE 'say map(chr, map { hex } split)'

微信扫一扫,分享到朋友圈

How can I use a Perl one-liner to decode an ASCII string encoded in hexadecimal?

利用这 3 种免费工具,轻松打造个人维基系统

上一篇

FBI警告用户小心黑客利用RDP协议发起攻击

下一篇

你也可能喜欢

How can I use a Perl one-liner to decode an ASCII string encoded in hexadecimal?

长按储存图像,分享给朋友