N
niall.macpherson
I needed a bit of code to convert a string to the hex values for the
individual chars and found the following 2 examples
my $teststr = 'abcABC123XYZ';
my $hexstr = join (' ', map (sprintf("%2.2x", ord($_)), split(//,
$teststr))); ## method 1
$hexstr = unpack 'H* ' => $teststr; ## method 2
Both of these work fine , although the second one doesn't separate the
values with spaces (which I needed) so I did
$hexstr =~ s/(\d\d)/$1 /g;
Method 1 was a useful exercise in understanding how map works (which I
previously hadn't been too comfortable with), but I decided to go with
method 2 as it looks a bit cleaner.
I think I understand how unpack works , however I have only previously
seen the => operator used in initialising hashes and I am not clear how
it works in this context. I looked at perldoc perlop but couldn't spot
anything relevant.
I'm therefore unsure how I can insert the spaces in method 2 in the
same line because I don't understand how / why the => operator is used
here.
Can someone please explain ?
Thanks
individual chars and found the following 2 examples
my $teststr = 'abcABC123XYZ';
my $hexstr = join (' ', map (sprintf("%2.2x", ord($_)), split(//,
$teststr))); ## method 1
$hexstr = unpack 'H* ' => $teststr; ## method 2
Both of these work fine , although the second one doesn't separate the
values with spaces (which I needed) so I did
$hexstr =~ s/(\d\d)/$1 /g;
Method 1 was a useful exercise in understanding how map works (which I
previously hadn't been too comfortable with), but I decided to go with
method 2 as it looks a bit cleaner.
I think I understand how unpack works , however I have only previously
seen the => operator used in initialising hashes and I am not clear how
it works in this context. I looked at perldoc perlop but couldn't spot
anything relevant.
I'm therefore unsure how I can insert the spaces in method 2 in the
same line because I don't understand how / why the => operator is used
here.
Can someone please explain ?
Thanks