Re: [MacPerl] Re: How can I display a number in dual representation, e.g. 29 --> 11101?
[email protected] (Richard Cook) Sat, 5 Mar 2005 11:18:32 -0800
| Newsgroups | perl.macperl |
|---|---|
| Message-ID | <[email protected]> |
And, don't forget you can use oct to go the other way, if you have the
0b prefix:
for my $n (0..255){
printf "%d\n", oct sprintf "0b%b", $n;
}
> oct Interprets EXPR as an octal string and returns the
> correspond-
> ing value. (If EXPR happens to start off with "0x",
> interprets
> it as a hex string. If EXPR starts off with "0b", it is
> inter-
> preted as a binary string. Leading whitespace is
> ignored in
> all three cases.) The following will handle decimal,
> binary,
> octal, and hex in the standard Perl or C notation:
>
> $val = oct($val) if $val =~ /^0/;
On Mar 4, 2005, at 7:35 PM, Chris Sarnowski wrote:
>
> 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
>>
>>
>>
>