in reply to convert hex to char fast

There is no ASCII character A0 or B1. If the data is arbitrary bytes, then the following will do:

pack('H*', '4142A0B1')

If the data is actually text (as implied by "ASCII"), you'll need to determine the actual encoding used and use

decode($enc, pack('H*', '4142A0B1'))

(The decode step can be skipped for iso-8859-1.)

For example,

use strict; use warnings; use open ':std', ':locale'; use Encode qw( decode ); for my $enc (qw( ASCII cp850 cp1252 iso-8859-1 UTF-8 )) { my $s = decode($enc, pack('H*', '4142A0B1'), sub { "?" }); printf("%-11s %s\n", "$enc:", $s); }
ASCII:      AB??
cp850:      ABá▒   Common "western" code page for Windows console
cp1252:     AB ±   Common "western" code page for Windows gui apps
iso-8859-1: AB ±   Common "western" unix encoding
UTF-8:      AB??

Note that cp1252 and iso-8859-1 are not equivalent despite producing the same output in this situation.

Replies are listed 'Best First'.
Re^2: convert hex to char fast
by Anonymous Monk on Mar 30, 2010 at 17:53 UTC
    Wow, perfect, pack('H*, $input) gives it a 600% performance increase over my regex.

    Thanks monks, I'm in nirvana now.

    Barry