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

[email protected] (Chris Sarnowski) Fri, 4 Mar 2005 22:35:22 -0500
Newsgroups perl.macperl
Message-ID <[email protected]>
You can use unpack.

Here is an example from MacOS X rather than macperl, but it should work 
the same way.

% perl -e 'print unpack("B*", chr(29)), "\n"'
00011101

note that in the context of unpack, values always seem to be treated as 
strings; that is
% perl -e 'print unpack("B*", 29), "\n"'
0011001000111001

prints the binary for '2' and '9'.

On Mar 4, 2005, at 6:30 AM, [email protected] wrote:

> ----------------------------------------------------------------------
>
> From: Detlef Lindenthal <[email protected]>
> Date: March 4, 2005 6:28:43 AM EST
> To: [email protected]
> Subject: How can I display a number in dual representation, e.g. 29 
> --> 11101?
>
>
> 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?
>
> Detlef Lindenthal
>
>
>