2009/07/14

Hex string to ASCII string in Perl

If you want to convert a string "ABCDEFG" to Hex mode like "07,41,42,43,44,45,46,47" -- a length prefix with all the others in HEX mode. How to do it in Perl?

You can do
$to = join(",", map { unpack('H2', $_) } chr(length($from)), split(/(?=.)/, $from));

or
$to = join(",", map { unpack('H2', $_) } split(/(?=.)/, pack('W/a', $from)));


How to do the reverse??? A question for you :P
发表评论