Skip to content

Perl: convert a String of chars to hex (and back)

Something that may come handy, if you need to convert ASCII to HEX and backward ...

use strict;
my $str="test";
print "'". $str. "'\n";
($str) =~ s/(.|\n)/sprintf("%02lx ", ord $1)/eg;
chop $str;
print "'". $str. "'\n";
($str) =~ s/([a-fA-F0-9]{2}) ?/chr(hex $1)/eg;
print "'". $str. "'\n";