[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


 
         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 <suppressed> 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


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.