in reply to Looking for regexp to convert hex numbers in text

try:
$line =~ s/(\[0x(.{2})\])/chr($2)/eg;

Replies are listed 'Best First'.
Re: Re: Looking for regexp to convert hex numbers in text
by Hofmator (Curate) on Nov 07, 2001 at 16:40 UTC

    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