You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Deepak Mallya <ma...@gmail.com> on 2007/03/04 23:43:20 UTC

Converting a String of bits into a binary number in perl?

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


Thanks,
Deepak

Re: Converting a String of bits into a binary number in perl?

Posted by Perrin Harkins <ph...@gmail.com>.
Guys, would you mind moving this question to a different forum?  It's
not really related to mod_perl.  There are some resources for general
perl help listed here:
http://perl.apache.org/docs/offsite/other.html

- Perrin

Re: Converting a String of bits into a binary number in perl?

Posted by Deepak Mallya <ma...@gmail.com>.
         the final value should be a binary value(datatype) not a string of
binary digits.
        eg:-  if we do $a=0b."100" this becomes a string and when we take
the length($a) it gives us the size in bytes of a integer (here it would
be 4 bytes)
                but if we do $a=0b100 its a binary value so length($a)
gives us the the size in bytes (so length would be 1 byte)
Consider
 $a=0b100;
$b=0b."100";
print length($a)."\n"; #gives 1
print length($b); # gives 4

But if we do this
print length(pack ("b*","100010111")); this gives length 2 as there are 9
bits here ...


On 3/6/07, David Nicol <da...@gmail.com> wrote:
>
> >  $a="100";  I want to convert this into perl's interpretation of
> > binary no ie $a=0b100
>
> # perl -wple 's/\b([01]+)\b/"0b$1"/gee'
> 100
> 4
> 10111101010101
> 12117
> the powers of 2 are 1, 10, 100, 1000, 10000, 100000
> the powers of 2 are 1, 2, 4, 8, 16, 32
>
>
> --
> "Big discoveries are protected by public incredulity." -- Marshall McLuhan
>

Re: Converting a String of bits into a binary number in perl?

Posted by David Nicol <da...@gmail.com>.
>  $a="100";  I want to convert this into perl's interpretation of
> binary no ie $a=0b100

# perl -wple 's/\b([01]+)\b/"0b$1"/gee'
100
4
10111101010101
12117
the powers of 2 are 1, 10, 100, 1000, 10000, 100000
the powers of 2 are 1, 2, 4, 8, 16, 32


-- 
"Big discoveries are protected by public incredulity." -- Marshall McLuhan

Re: Converting a String of bits into a binary number in perl?

Posted by to...@tuxteam.de.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, Mar 04, 2007 at 04:43:20PM -0600, Deepak Mallya wrote:
> 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

Easy. See "man perlfunc", especially "pack", "unpack" and "vec" (there
are actually some examples there). Basically:

 # makes the binary representation of an integer:
 | tomas@herr-turtur:~$ perl -e 'print unpack("b*", 100), "\n"'
 | 100011000000110000001100
 # makes an integer out of a binary representation:
 | tomas@herr-turtur:~$ perl -e 'print pack("b*", "100011000000110000001100"), "\n"'
 | 100

HTH
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFF66bLBcgs9XrR2kYRApv5AJ9n11VhhCnOfHTf7HOyY4Y2qEpL1QCfesdg
kCHWCs1W5sP/4souLe/fdLE=
=bjdu
-----END PGP SIGNATURE-----


Re: Converting a String of bits into a binary number in perl?

Posted by Steven Lembark <le...@wrkhors.com>.

-- Deepak Mallya <ma...@gmail.com>

>
> 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
lembark@wrkhors.com                                     1 888 359 3508

Re: Converting a String of bits into a binary number in perl?

Posted by Rhett Creighton <rh...@creighton.com>.
I think that you want to use pack or unpack.  Make sure that you get the 
templates right.  Those are tricky functions.

Rhett

On Sun, 4 Mar 2007, Deepak Mallya wrote:

> 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
> 
> 
> Thanks,
> Deepak
>