Re: [MacPerl] Re: How can I display a number in dual representation, e.g. 29 --> 11101?
[email protected] (Richard Cook) Sun, 6 Mar 2005 12:15:13 -0800
| Newsgroups | perl.macperl |
|---|---|
| Message-ID | <[email protected]> |
On Mar 5, 2005, at 2:05 PM, Bruce Van Allen wrote:
> On 2005-03-05 Richard Cook wrote:
>> 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;
>> }
>
> Good. Maybe you also meant
> printf "%b\n", oct ...
> ^
> as a way to show the binary value.
Well, no, you don't need oct() for that. You can just do
for my $n (0..255){
printf "%08b\n", $n;
}
I meant to use oct() to print $n in decimal. But, if you're the kind of
person who likes a script to print something non-decimal ...
for my $n (0..255){
printf "%d %X %o %b\n", (oct sprintf "0b%b", $n) x 4;
}
:-)
-Richard
>
>
>>> 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/;
>>
> - Bruce
>
> __bruce__van_allen__santa_cruz__ca__
>