Bill,

I get an IP in Hex format: 0xc0ad0100 whose decimal is 3232235776

I have to convert it into a dotted notation. You said the following code
will work

my $dot_ip = sprintf "%u.%u.%u.%u", @byte;
print "dot_ip=$dot_ip\n";


This assumes the @byte array has four elements each representing one
octet.
But how do I populate @byte array, from the above IP?


Thanks for correcting my code - I can now count subnet and host bits
separately for each subnet mask. You mentioned that there is another way
of converting the mask to all ones and counting characters.
Can you share this? Hopefully that will help me in other areas that I am
working on.

Thanks again for the help,

-----Original Message-----
From: $Bill Luebkert [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, June 15, 2004 4:42 PM
To: Viswanatha Rao
Subject: Re: bitwise AND operation on IP addr and subnet mask in perl

Viswanatha Rao wrote:

> Bill,
> Bill,
> 
> Thank you. I would now like to convert the binary IP into dotted
decimal
> notation. I was thinking of taking 4 bytes and printing like this.
> 
> $dot_ip = sprintf "%d.%d.%d.%d",@byte[0],@ byte[1],@ byte[2],@
byte[3];
> To get each byte I have to get to hex version and select 2 digits at a
> time. 
> 
> Can you tell me how

Either of these should work (depending on format you want):

my $dot_ip = sprintf "%u.%u.%u.%u", @byte;
print "dot_ip=$dot_ip\n";

$dot_ip = join '.', @byte;
print "dot_ip=$dot_ip\n";

> Another piece of code I am trying to do is count actual number of
> subnets and hosts based on subnet mask. But the following code does
not
> work.
> 
> # convert mask into Hex format
> $temp = sprintf "%08x", $mask;
> for ($j=0;$j<31; $j++)
> {
>       if ( ($temp & (1 << $j)) == 1) {
>               $count_subnet_bits = $count_subnet_bits + 1;
>       }
>       if ( ($temp ^ (1 << $j)) == 1){
>               $count_host_bits = $count_host_bits + 1;
>       }
> }
> print "count subnets: $count_subnet_bits\n";
> print "count hosts: $count_host_bits\n";
> 
> Can you tell what am I doing wrong?

Not sure what you are doing.  Are you just counting the 1's in the mask
to determine the network mask bits ?

Using your code technique :

my $count_subnet_bits = 0;
my $count_host_bits = 0;
for (my $j = 0; $j < 32; $j++) {
        if ($mask & (1 << $j)) {
                $count_subnet_bits++;
        } else {
                $count_host_bits++;
        }
}
print "count subnets: $count_subnet_bits\n";
print "count hosts: $count_host_bits\n";

There are other ways to do it like converting to a string of ones
and counting the characters, etc.


PS: You need to start posting these to the list instead of to me direct.
-- 
  ,-/-  __      _  _         $Bill Luebkert
Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic
http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to