### 2.15.3. Discussion

The `oct` function converts octal numbers with or
without the leading "`0`"; for example,
"`0350`" or "`350`". Despite its
name, `oct` does more than convert octal numbers: it
also converts hexadecimal ("`0x350`") numbers if
they have a leading "`0x`" and binary
("`0b101010`") numbers if they have a leading
"`0b`". The `hex` function converts
only hexadecimal numbers, with or without a leading
"`0x`": "`0x255`",
"`3A`", "`ff`", or
"`deadbeef`". (Letters may be in upper- or
lowercase.)

Here's an example that accepts an integer in decimal, binary, octal,
or hex, and prints that integer in all four bases. It uses the
`oct` function to convert the data from binary,
octal, and hexadecimal if the input begins with a 0. It then uses
`printf` to convert into all four bases as needed.

print "Gimme an integer in decimal, binary, octal, or hex: ";
$num = <STDIN>;
chomp $num;
exit unless defined $num;
$num = oct($num) if $num =~ /^0/; # catches 077 0b10 0x20
printf "%d %#x %#o %#b\n", ($num) x 4;