Re: perlfaq4 - tweaks for "How do I convert between numeric representations"
[email protected] (Iain Truskett)
| Newsgroups | perl.perlfaq.workers |
|---|---|
| Message-ID | <[email protected]> |
* Philip Newton ([email protected]) [25 Aug 2003 02:01]: > On Sat, 23 Aug 2003 23:33:10 +1000, [email protected] (Iain Truskett) > wrote: > > Attached are some diffs that accomplish the following things for > > the radix conversion. > [snip] > > 5. Mention ord's ability to convert binary numbers. > Um, ord's ability to convert binary numbers? Bugger! > I presume you were thinking about oct()? Yes, I was. Thank you noting that. Attached patch fixes for current cvs. cheers, -- Iain.
radix2.patch
(text/plain, 935 B)
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.46
diff -U10 -r1.46 perlfaq4.pod
--- perlfaq4.pod 24 Aug 2003 05:35:31 -0000 1.46
+++ perlfaq4.pod 25 Aug 2003 03:29:22 -0000
@@ -190,24 +190,24 @@
$vec = Bit::Vector->new_Dec(32, -559038737);
$oct = reverse join('', $vec->Chunk_List_Read(3));
=item How do I convert from binary to decimal
Perl 5.6 lets you write binary numbers directly with
the 0b notation:
$number = 0b10110110;
-Using ord:
+Using oct:
my $input = "10110110";
- $decimal = ord( "0b$input" );
+ $decimal = oct( "0b$input" );
Using pack and ord:
$decimal = ord(pack('B8', '10110110'));
Using pack and unpack for larger strings:
$int = unpack("N", pack("B32",
substr("0" x 32 . "11110101011011011111011101111", -32)));
$dec = sprintf("%d", $int);