Perl has a hex statement that does the decoding logic for you.
hex EXPR
hex
Interprets EXPR as the sixth string and returns the corresponding value. (To convert lines that can begin with 0 , 0x or 0b , see oct .) If EXPR is omitted, uses $_ .
print hex '0xAf';
Remember that the default split behavior breaks the line in space separators, for example
$ perl -le '$ _ = "abc"; print for split '
a
b
c
For each line of input, divide it into hexadecimal values, convert the values ββto numbers and push them into an array for later processing.
#! /usr/bin/perl use warnings; use strict; my @values; while (<>) { push @values => map hex($_), split; } # for example my $sum = 0; $sum += $_ for @values; print $sum, "\n";
Run Example:
$ ./sumhex mtanish-input
4196
source share