Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

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?

like image 494
zoul Avatar asked Feb 18 '10 08:02

zoul


1 Answers

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)'
like image 183
Hans W Avatar answered Oct 27 '22 21:10

Hans W