Re: [MacPerl] How can I display a number in dual representation, e.g. 29 --> 11101?

[email protected] (Bart Lateur) Fri, 04 Mar 2005 13:50:26 +0100
Newsgroups perl.macperl
Organization MediaMind
Message-ID <[email protected]>
On Fri, 04 Mar 2005 12:28:43 +0100, Detlef Lindenthal wrote:

>What would be the easiest way to display a number as dual / binary number?
>For example: 29 is to be displayed as 11101.
>
>
>In PHP this would work:
><?
>printf ("%b", 29);
>?>
>
>but not so in Perl.
>Any idea?

What version of Perl are you using? "%b" works in perl5.8.x, and,
judging by the "perldoc -f sprintf" for 5.6.1, in 5.6.1 too. So unless
you have a very old MacPerl, it should work.

Otherwise, if you don't mind some leading zeroes, and only need it for
32 bit max numbers, you can still do

	print unpack 'B*', pack 'N', 29;

You can get rid of the leading zeroes by a

	s/^0+(?=\d)//

or

	s/^0+(\d)/$1/

in case lookahead is too recent.

-- 
	Bart.