one-liners beginner

How to convert hex to decimal with perl one-liner?

Question

How to convert hex to decimal with perl one-liner?

Converting Hexadecimal to Decimal with a Perl One-Liner

To convert a hexadecimal number to decimal in Perl, the built-in hex function is the simplest and most idiomatic choice. It takes a hex string (optionally starting with 0x) and returns the decimal value as a number.

A common mistake when running Perl one-liners inside a limited sandbox or an environment that pipes code to perl - (reading from STDIN rather than command line arguments) is trying to use the -E or -e flags with additional command-line arguments. These flags expect code as parameters, and command-line arguments after the code are separate from standard input, which may cause parsing errors.

To write a runnable, self-contained example that works with perl - (reading from STDIN), you can read the hex number from standard input and print the decimal equivalent. This approach avoids using -E flags and command-line args that cause issues in restricted environments.

Runnable Perl Example (reading hex from STDIN)

use strict;
use warnings;

my $hex = <>;          # Read one line from STDIN
chomp $hex;
my $dec = hex($hex);
print "$dec\n";

This script reads one line of input (the hex string), removes the trailing newline, converts it to decimal using hex, then prints the result.

How to Run This with perl -

Save the code in a file, or run it directly by piping input like this:

echo "1a3f" | perl -

Output:

6719

Explanation and Key Points

  • hex converts strings containing only hexadecimal digits (0-9A-Fa-f) optionally preceded by 0x or 0X.
  • Perl context: hex returns a numeric decimal scalar in scalar context.
  • Using perl -E 'say hex(shift)' 1a3f works fine when running from a shell directly, but does not work with perl - expecting code on standard input.
  • To read command-line arguments instead of STDIN, you normally use -E or -e with inline code, but sandbox restrictions or your execution environment may break that.
  • Hence, reading from STDIN is the safest approach when using perl - with no additional arguments.

Common Pitfalls

  • Confusing running Perl one-liners with perl -e/-E and perl - (which expects source code on STDIN).
  • Not chomping input lines, resulting in unexpected whitespace or linebreaks.
  • Passing unquoted hex with shell special characters like 0x, which the shell might interpret differently.

Summary

In environments where you must run Perl code with perl - (reading source from STDIN), convert hex to decimal by reading from STDIN, using hex, then printing the result:

my $hex = <>;
chomp $hex;
print hex($hex), "\n";

This approach reliably works without needing command-line arguments or extra Perl flags.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 21ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
0
STDERR
Use of uninitialized value $hex in scalar chomp at - line 5.
Use of uninitialized value $hex in hex at - line 6.

Was this helpful?

Related Questions