-- Deepak Mallya <[EMAIL PROTECTED]>
Hi,
Can anyone tell me how do I convert a String of bits into a binary
number in Perl
For eg:- $a="100"; I want to convert this into perl's interpretation of
binary no ie $a=0b100
You have to mask the value to 32 bits and then use
"unpack". Issue is that with a string you may have
more than the upper limit of bits, which will lead
to overflows (or having to use BigIntegers ).
One apprach that uses a simpler pack string is
my @intz = ();
while( my $bits = substr $bitstring, -32, 32, '' )
{
push @intz, pack $pack_format, $bits;
}
that or
my $bits = substr $bitstring, -32, 32, '';
my $int = $pack_format, $bits;
carp "Oversize bitstring: '$bitstring' remaining after $int";
Note that the substr and pack options have to be
consistent with the storage on the architecture
you are using (low- or high-endian) or you have
to force the bits into network order before making
the bitstring.
--
Steven Lembark 85-09 90th Street
Workhorse Computing Woodhaven, NY 11421
[EMAIL PROTECTED] 1 888 359 3508