Title: Convert int to bitstring
    Here is one using printf and you could use sprintf:
 
$_ = "255.255.255.224";
 
foreach ( split(/\./, $_ ) ) {
    printf "%b\n", $_;
 }
 
Output:
11111111
11111111
11111111
11100000
 
Wags ;)
-----Original Message-----
From: Bullock, Howard A. [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 18, 2002 10:26
To: [EMAIL PROTECTED]
Subject: Convert int to bitstring

I want to convert int(255) to a string of 8 ones (11111111). I have been trying to use unpack, but am missing something basic I think.

I was also playing with the following with less than successful results. What am I doing wrong? One guess was the the @mask elements are string values and the bitwise operation are not performing as I initially expected.

$mask = "255.255.255.224";

@mask = split(/\./,$mask,4);

$mymaskval = ($mask[0]<<24)|($mask[1]<<16)|($mask[2]<<8)|$mask[3];

$setbits = unpack("%32b*", $mymaskval);


I have read the unpack docs but did not see references to the "%" and "*" in "%32b*". Can someone explain this as well.

Howard A. Bullock

Global IT Infrastructure

717-810-3584



**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************

Reply via email to