Jump to content

Convert Binary, Octal, and Hexidecimal Numbers in Perl

VOTE
+ 4
  • -
  • +
  gnat's Photo
Posted Oct 20 2009 05:10 PM

Perl understands numbers specified in binary (base-2), octal (base-8), and hexadecimal (base-16) notation only when they occur as literals in your programs. If they come in as data—such as by reading from files or environment variables, or when supplied as command-line arguments—no automatic conversion takes place.

If you want to convert a string (e.g., "0b10110", "0x55", or "0755") containing a binary, octal, or hexadecimal number to the correct number, use Perl's hex function if you have a hexadecimal string like "2e" or "0x2e":

$number = hex($hexadecimal);         # hexadecimal only ("2e" becomes 47)


Use the oct function if you have a hexadecimal string like "0x2e", an octal string like "047", or a binary string like "0b101110":

$number = oct($hexadecimal);         # "0x2e" becomes 47 
$number = oct($octal);               # "057" becomes 47 
$number = oct($binary);              # "0b101110" becomes 47


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 %#bn", ($num) x 4;


The # symbol between the percent and the three non-decimal bases makes printf produce output that indicates which base the integer is in. For example, if you enter the number "255", the output would be:

255 0xff 0377 0b11111111


But without the # sign, you would only get:

 255 ff 377 11111111


The following code converts Unix file permissions. They're always given in octal, so we use oct instead of hex.

print "Enter file permission in octal: ";
$permissions = <STDIN>;
die "Exiting ...n" unless defined $permissions;
chomp $permissions;
$permissions = oct($permissions);   # permissions always octal
print "The decimal value is $permissionsn";


Cover of Perl Cookbook
Learn more about this topic from Perl Cookbook, Second Edition.  Find a Perl programmer, and you'll find a copy of Perl Cookbook nearby. Perl Cookbook is a comprehensive collection of problems, solutions, and practical examples for anyone programming in Perl. The book contains hundreds of rigorously reviewed Perl "recipes" and thousands of examples ranging from brief one-liners to complete applications. The second edition of Perl Cookbook has been fully updated for Perl 5.8, with extensive changes for Unicode support, I/O layers, mod_perl, and new technologies that have emerged since the previous edition of the book. Recipes have been updated to include the latest modules. New recipes have been added to every chapter of the book, and some chapters have almost doubled in size.
Learn More Read Now on Safari







0 Alternative Solutions | 0 Comments

filter by: