Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting binary to decimal in PERL

Posted on 2010-09-06
11
Medium Priority
?
917 Views
Last Modified: 2012-06-21
I need some help writing a program in PERL that will convert input from the user from binary #, ie 0 or 1, into it's decimal equivalent.  I can not use the pack/unpack functions though.  it must be able to handle up to 20 characters and Use strict must be applied.  iI'm not sure where to start for this one.
0
Comment
Question by:PMG76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 8

Expert Comment

by:pvlier
ID: 33612466
Say
$x_bin="0001001100101"
$x_dec=613
so you need to convert $x_bin into $x_dec. Use oct():
$x_dec = oct("0b".$x_bin);

Quoting from man perlfunc:oct EXPR oct Interprets EXPR as an octal string and returns the corresponding value. (If EXPR happens to start off with "0x", interprets it as a hex string. If EXPR starts off with "0b", it is interpreted as a binary string. Leading whitespace is ignored in all three cases.)

You can also use Bit::Vector:
use Bit::Vector;
my $v = Bit::Vector->new_Bin( 32, '0001001100101' );
print "dec: ", $v->to_Dec(), "\n";

0
 

Author Comment

by:PMG76
ID: 33613214
I have to get input for the user and spit out the resulting decimal number.  I'm not sure how to implement your description into my code.  It's kind of confusing.
0
 
LVL 84

Expert Comment

by:ozo
ID: 33613473
What are you confused about?

If you post your code, we can more easily determine how to integrate the above code into it.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:PMG76
ID: 33613479
Here is what Ive come up with so far.  When I am entering 20 digit binary numbers tho the hex numbers are a little off.  For example.  Enter a binary number up to 20 digits: 11111111111111111111
11111111111111111111 is 1048577 in decimal
The correct answer should be 1048575.
#!/usr/bin/perl 

use warnings;
use strict;
print "\n";
print 'Enter a binary number up to 20 digits: ' ;
chomp ( my $num = <> );

my $pow=1;
my $dec=0;
{
my $bin=$num;
until($num==0)
{
my $bit=$num%10;
$dec=$dec+($bit*$pow);
$pow=$pow*2;
$num=$num/10;
}
print("$bin is $dec in decimal\n");
$pow=1;
$dec=0;
}

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 33613488
#!/usr/bin/perl

use warnings;
use strict;
print "\n";
print 'Enter a binary number up to 20 digits: ' ;
chomp ( my $num = <> );
my $dec = oct("0b$num");
print("$num is $dec in decimal\n");
0
 
LVL 84

Expert Comment

by:ozo
ID: 33613528
To see the problem with your method, add
printf "%.9f\n",$num;
to your loop
0
 
LVL 8

Expert Comment

by:pvlier
ID: 33617566
hmmm... that was my solution but no points for me...  :-)
0
 

Author Comment

by:PMG76
ID: 34734190
OZO

I am looking at my code for this problem again and need some help.  Looking at my original code, not your code using oct, what do I need to correct to get the correct results?  I get the right answer when using eleven 1's, but not when using twenty 1's.
0
 
LVL 84

Expert Comment

by:ozo
ID: 34734394
You would need to treat $num as a string rather than as a decimal number.
As a decimal number, twenty 1's exceeds the precision of the floating point numbers on your machine.

Either that or use an extended precision module.
0
 

Author Comment

by:PMG76
ID: 34734467
It's been awhile since I have worked in perl.  How do I modify this to make it a string?
0
 
LVL 84

Expert Comment

by:ozo
ID: 34734587
#!/usr/bin/perl

use warnings;
use strict;
print "\n";
print 'Enter a binary number up to 20 digits: ' ;
chomp ( my $num = <> );
{
my $pow=1;
my $dec=0;
my $bin=$num;
until(!$num)
{
my $bit=chop $num;
$dec=$dec+($bit*$pow);
$pow*=2;
}
print("$bin is $dec in decimal\n");
}
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question