jepri has asked for the wisdom of the Perl Monks concerning the following question:

I have a program that generates a text file interspersed with the unprintable characters turned into hex codes in the form [0xff]

I have been trying to write a one shot regexp that would trawl through the entire document and convert the hex codes back into the characters, but this code is the best I can come up with.

Can anyone see a better way to do it?

while ( my ( $hex ) = ($line =~ m/\[0x(..?)\]/)) { my $chr = pack("h*", $hex); #print "$line \n Found hex value '$hex', equals char $chr\n"; $line =~ s/\[0x$hex\]/$chr/; }

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
Re: Looking for regexp to convert hex numbers in text
by rob_au (Abbot) on Nov 06, 2001 at 18:11 UTC
    From URI::Escape ...

    s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg

    ... which can easily be adapted, thus ...

    s/\[0x([\da-fA-F]{2})\]/chr(hex($1))/eg

     

    Ooohhh, Rob no beer function well without!

Re: Looking for regexp to convert hex numbers in text
by busunsl (Vicar) on Nov 06, 2001 at 18:13 UTC
    try:
    $line =~ s/(\[0x(.{2})\])/chr($2)/eg;

      Two things:

      1. This doesn't work, you have to use chr(hex($2))otherwise the numbers are converted as integer values and not hex!
      2. You don't need two pairs of capturing parens, this is unnecessary extra work for the regex engine.
      So the correct solution is s/\[0x([a-f0-9A-F]{1,2})\]/chr(hex($1))/eg;

      -- Hofmator

Re: Looking for regexp to convert hex numbers in text
by Zaxo (Archbishop) on Nov 06, 2001 at 18:21 UTC
    s/(0x[0-9a-fA-F]+)/chr(oct($1))/ego;

    After Compline,
    Zaxo

Re: Looking for regexp to convert hex numbers in text
by staeryatz (Monk) on Nov 07, 2001 at 02:08 UTC
    From where I learned Perl, you can do this:
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    You can use pack "C" here, to convert to unsigned chars. I use this as a part of URL decoding in CGI scripts.

    This packs a pair of Hex characters into an unsigned character, using the substitute function.