ZetCode

Perl hex Function

last modified October 8, 2026

The Perl hex function converts hexadecimal strings to decimal numbers. It interprets a string as a hex number and returns its decimal equivalent.

hex is useful when working with hexadecimal values from files, user input, or network protocols. It handles both uppercase and lowercase hex digits (A-F or a-f).

Basic hex Usage

The simplest way to use hex is with a hexadecimal string.

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $hex_value = "FF";
my $decimal = hex($hex_value);

print "Hexadecimal $hex_value is decimal $decimal\n";

We convert the hexadecimal string "FF" to its decimal equivalent. The function returns 255, which is FF in base 16.

$ ./basic.pl
Hexadecimal FF is decimal 255

Hexadecimal Prefix Handling

hex automatically ignores the common "0x" prefix.

prefix.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $hex1 = "0xFF";
my $hex2 = "FF";

print "With prefix: ", hex($hex1), "\n";
print "Without prefix: ", hex($hex2), "\n";

Both forms produce the same result. The function accepts an optional "0x" or "0X" prefix, but it does not skip leading whitespace.

$ ./prefix.pl
With prefix: 255
Without prefix: 255

Case Insensitivity

hex works with both uppercase and lowercase hex digits.

case.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $upper = "ABCD";
my $lower = "abcd";

print "Uppercase: ", hex($upper), "\n";
print "Lowercase: ", hex($lower), "\n";

The function treats 'A' and 'a' identically, converting both to decimal 43981. This makes it flexible with different input formats.

$ ./case.pl
Uppercase: 43981
Lowercase: 43981

Invalid Characters

hex stops conversion at the first invalid character.

invalid.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $valid = "1A3F";
my $invalid = "1G3F";

print "Valid: ", hex($valid), "\n";
print "Invalid: ", hex($invalid), "\n";

The function converts "1G3F" as just "1" since 'G' isn't a hex digit. The conversion stops at the invalid character. With use warnings, Perl also prints a warning to the standard error output.

$ ./invalid.pl
Valid: 6719
Illegal hexadecimal digit 'G' ignored at ./invalid.pl line 11.
Invalid: 1

Hexadecimal to Binary

Combine hex with sprintf for binary conversion.

binary.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $hex = "A5";
my $decimal = hex($hex);
my $binary = sprintf("%08b", $decimal);

print "Hex $hex is binary $binary\n";

We first convert to decimal, then format as 8-bit binary. The %08b format ensures leading zeros for consistent 8-digit output.

$ ./binary.pl
Hex A5 is binary 10100101

Reading Hex from File

hex is useful when processing hexadecimal data from files.

file.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

open(my $fh, '<', 'hexdata.txt') or die "Can't open file: $!";

while (my $line = <$fh>) {
    chomp $line;
    print "$line = ", hex($line), "\n";
}

close($fh);

This script reads hex values from the file hexdata.txt, which contains one value per line, and converts each to decimal. The chomp removes newlines before conversion.

$ cat hexdata.txt
FF
1A
7f
$ ./file.pl
FF = 255
1A = 26
7f = 127

Hexadecimal Arithmetic

hex enables arithmetic operations on hex strings.

arithmetic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $hex1 = "A";
my $hex2 = "5";

my $sum = hex($hex1) + hex($hex2);
print "$hex1 + $hex2 = $sum (decimal)\n";

my $hex_sum = sprintf("%X", $sum);
print "Sum in hex: $hex_sum\n";

We convert hex strings to decimal, perform addition, then format the result back to hexadecimal. This demonstrates complete hex/decimal conversion.

$ ./arithmetic.pl
A + 5 = 15 (decimal)
Sum in hex: F

Best Practices

Source

Perl hex Documentation

This tutorial covered Perl's hex function with practical examples demonstrating hexadecimal to decimal conversion.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.