Blog of Leonid Mamchenkov

You just stepped in a pile of posts.

Decimal to binary convertion in Perl

Posted in All on January 26th, 2006 · 3 Comments

This is a simple thing, but when you need it - you need it. There is no need in implementing a function that does or looking for a module at CPAN. All you are looking for is already there.

#!/usr/bin/perl -w

use strict;

# Get the number from the command line or use default.
my $number = shift || 42;

printf "%b\n", $number;

You might want to refresh you memory of printf or sprintf by flipping though the manual pages.

P.S.: 42 decimal = 101010 binary. Is it cool or what?

Tags: , , , , , , ,

3 responses so far ↓

  • 1 Randal L. Schwartz // Jan 26, 2006 at 6:03 pm

    You don’t need the parens after printf, or the 0 between the % and the b.

  • 2 Leonid Mamchenkov // Jan 26, 2006 at 7:34 pm

    Hi Randal, thanks for stopping by. You are right. I updated the post.

  • 3 Malcolm // Jan 27, 2006 at 1:54 pm

    What about the Cypriot reaction to their victorious tennis superstar way down in Melbourne, Australia. I heard Limassol went bananas about their boy Marcos.

Leave a Comment

Note: This post is over 2 years old. You may want to check later in this blog to see if there is new information relevant to your comment.