Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

count number of elements in an array and make it 12 bits and take 4 at a time and convert into decimal

0 views
Skip to first unread message

hara

unread,
May 24, 2006, 9:49:22 AM5/24/06
to
#!\c\perl\bin
print "Enter Bus no :";
chomp($bus = <STDIN>);

# convert bus no. to binary
system("cls");
@array;
$rem = 0;
while($bus>=2)
{
$rem = $bus % 2;
$bus = int($bus/2);
push(@array,$rem);
}

push(@array,$bus);
@bus = reverse(@array);
print "The binary representation of BUS ID is : @bus \n";
#here i want to convert the binary number into 8 bits by putting '0' on
the left.
$bus1=length("@bus");
print "$bus1\n";
if ($bus1==1)
{
@bus="0000000@bus";
}
elsif($bus1==2)
{
@bus="000000@bus";
}
elsif($bus1==3)
{
@bus="00000@bus";
}
else
{
@bus="0000@bus";
}
print "@bus\n";

But this is not working?
then i want to convert that 8 bits (by taking 4 bits at a time) to
decimal.
Can any body suggest anything?
how to count the length of the @bus variable.
and then add zeros on it's left?

David Squire

unread,
May 24, 2006, 10:05:30 AM5/24/06
to
hara wrote:
> #!\c\perl\bin

Missing:

use strict;
use warnings;

> print "Enter Bus no :";
> chomp($bus = <STDIN>);
>
> # convert bus no. to binary
> system("cls");

What is the following line doing?

> @array;
> $rem = 0;
> while($bus>=2)
> {
> $rem = $bus % 2;
> $bus = int($bus/2);
> push(@array,$rem);
> }
>
> push(@array,$bus);
> @bus = reverse(@array);
> print "The binary representation of BUS ID is : @bus \n";
> #here i want to convert the binary number into 8 bits by putting '0' on
> the left.
> $bus1=length("@bus");

That is an extremely uninformative variable name. Why not $length_bus?

> print "$bus1\n";
> if ($bus1==1)
> {
> @bus="0000000@bus";
> }
> elsif($bus1==2)
> {
> @bus="000000@bus";
> }
> elsif($bus1==3)
> {
> @bus="00000@bus";
> }
> else
> {
> @bus="0000@bus";
> }
> print "@bus\n";
>
> But this is not working?

Take the advice you were given yesterday. You *do* *not* *need* to
convert the variable to binary yourself. Perl's output formatting
functions will do it for you:

----

#!/usr/bin/perl
use strict;
use warnings;

my $Bus = 13;

# Now let's create an eight character binary representations of that.

my $BinaryBusString = sprintf "%08b", $Bus;
print "The eight bit binary representation of $Bus is: $BinaryBusString\n";

----

Output:

The eight bit binary representation of 13 is: 00001101

> then i want to convert that 8 bits (by taking 4 bits at a time) to
> decimal.
> Can any body suggest anything?

I suspect you mean hexadecimal, since not all 4 bit binary numbers have
a (one character) decimal representation. Again, sprintf (or printf) is
your friend:

----

#!/usr/bin/perl
use strict;
use warnings;

my $Bus = 13;
printf "The two character hexadecimal representation of $Bus is:
%02x\n", $Bus;

----

Output:

The two character hexadecimal representation of 13 is: 0d


That's the last reply from me until you show some evidence of having
listened to the copious advice you have been given here.

DS

A. Sinan Unur

unread,
May 24, 2006, 10:33:51 AM5/24/06
to
> hara wrote:

>> #!\c\perl\bin

What is the point of this? On Windows, you can get away with putting
anything on the shebang line, but why so useless.

If you really wanted to use a Windows specific shebang line, you should
have used:

#!C:/Perl/bin/perl.exe

because that line is supposed to point to the interpreter that is going to
run the rest of the script.

However, I find it convenient to use

#!/usr/bin/perl

for easy back and forth between Windows and *nix machines.

Sinan

--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Ben Bullock

unread,
May 24, 2006, 9:50:31 PM5/24/06
to
"David Squire" <David....@no.spam.from.here.au> wrote in message
news:e51p7b$foh$1...@news.ox.ac.uk...

> That's the last reply from me until you show some evidence of having
> listened to the copious advice you have been given here.

Fair enough, but didn't he do the thing he was asked to do yesterday, post a
working code sample? He seems to be trying to follow the guidelines.

David Squire

unread,
May 25, 2006, 4:30:24 AM5/25/06
to

He has completely ignored all the advice to use (s)printf to format his
binary numbers. The latest one, converting a number to an array of 1s
and 0s for the binary representation, shows just how much ignoring is
going on.

Now, why would you persist with doing something the hard way when quite
a few people have pointed out a simple built-in solution? Do I smell an
assignment?

DS

0 new messages