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

H

hara

#!\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?
 
D

David Squire

hara said:
#!\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

A. Sinan Unur

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 <[email protected]>
(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
 
B

Ben Bullock

David Squire said:
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.
 
D

David Squire

Ben said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top