Perl and binary workings

Perhaps it's me - maybe I'm dumb and am getting this working solution wrong...

I have a binary value 00000011 in $binVal and I want to print the result in denary, so in perl I did as the perldoc -f oct told me to do and added a 0b prefix as so:

$binVal = "0b" . $binVal;

then I wanted to print the result, so I did:

print oct( $binVal );

If my binary calculations are right, I should get "3", instead I get "0" - why? where am I going wrong?

Cheers!

I get 3 .....

$binVal = "000011";
$binVal = "0b" . $binVal;
print oct($binVal);

:confused:

Which version of Perl are you running? Which Unix?

GNU/Linux 2.4.10
perl 5.8.0

I run the three lines exactly as quoted and got 3.

Ok - this is VERY bizarre!!!

I posted my question from work - where I was using a Win98 box and Perl 5.004 (or similar!)

I've just gotten home and have tried the same code on my FreeBSD box using Perl 5.6.1 and it worked perfectly :slight_smile:

Perhaps it was omitted in the Windows version 5.004? :confused:

Thanks for all your help though :wink:

This is what I get on my Win32 machine at work when I tested out the previous code:

D:\>cat test.pl
$binVal = "000011";
$binVal = "0b" . $binVal;
print oct($binVal);

D:\>test.pl
3
D:\>perl -version

This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2001, Larry Wall

Binary build 633 provided by ActiveState Corp. http://www.ActiveState.com
Built 21:33:05 Jun 17 2002


Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.