perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@tuxteam.de
Subject Re: Converting a String of bits into a binary number in perl?
Date Mon, 05 Mar 2007 05:12:43 GMT
-----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-----


Mime
View raw message