in reply to hex escape Question

It's not clear what the user inputs. The following code handles the strings 01, 42, etc.:
print map chr hex, qw( 01 42 62 00 01 );

Update: To use pack, you can

print map { pack 'H2', $_ } qw( 01 42 62 00 01 );

or

print pack 'H*', '0142620001';
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: hex escape Question
by Monk::Thomas (Friar) on Jul 28, 2015 at 18:05 UTC
    another variation:
    my @array = qw(01 42 62 00 01); print pack 'H2' x @array, @array;

      I was trying to capture it something like this:

      my $sfreq=<STDIN>; #014.262.00 my ($Mhz,$Khz,$hz)=split(/\./,$sfreq); my $h1=substr($Mhz,0,2); #h1=pack('H*',$h1); my $h2=substr($Mhz,2,1).substr($Khz,0,1); #$h2=pack('H*',$h2); my $h3=substr($Khz,1,2); #$h3=pack('H*',$h3); my $h4=substr($hz,0,2); my $hfreq="$h1$h2$h3$h4";
        Recommendation - It is safer to do the initial split like this:
        if ($sfreq =~ /^(\d)[.](\d)[.](\d)$/) { my $MHz = $1; my $KHz = $2; my $hz = $3; # rest of code } else { die "Sorry, invalid input. Please use format '<MHz>.<KHz>.<Hz>'.\n" }

        I must apologize, the problem wasn't with the hex, but on the serial side. Thank you for the help, at least you all cleared up the my confusion on the hex side.

        my $sfreq=<STDIN>; $sfreq=~ s/\.//g; $port->write(pack("H*",$sfreq."01"));