The Perl hex Function

The hex function is used to convert a hex string into the numerical corresponding value.

Two syntax forms are available for the Perl hex function:

hex EXPR
hex
This function:
  • has as argument a hexadecimal string (or an expression which after evaluation will return a hex string)
  • will return the decimal corresponding value

In the second syntax form of the Perl hex function, the expression argument is omitted and the function will use by default as argument the hexadecimal value stored in the special variable $_.

You can see a simple example here:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a scalar variable with an integer value
my $dec = hex("a23f");
 
print "$dec\n";
# it prints: 41535