Re: perlfaq4 - tweaks for "How do I convert between numeric representations"

[email protected] (Philip Newton)
Newsgroups perl.perlfaq.workers
Organization very little
Message-ID <[email protected]>
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?

I presume you were thinking about oct()?

ord("0b1010") is 48 (at least on an ASCII machine), as is
ord("0b1101010") or ord("0big blue monster").

Specifically, in this hunk of the patch:


@@ -199,13 +195,18 @@ Using Bit::Vector
 Perl 5.6 lets you write binary numbers directly with
 the 0b notation:
 
-	$number = 0b10110110;
+    $number = 0b10110110;
+
+Using ord:
+
+    my $input = "10110110";
+    $decimal = ord( "0b$input" );
 
-Using pack and ord
+Using pack and ord:
 
     $decimal = ord(pack('B8', '10110110'));
 
-Using pack and unpack for larger strings
+Using pack and unpack for larger strings:
 
     $int = unpack("N", pack("B32",
 	substr("0" x 32 . "11110101011011011111011101111", -32)));


I'm fairly sure that you need s/Using ord:/Using oct:/ as well as
s/ord( "0b/oct( "0b/ .

(The pack example, on the other hand, *does* need ord() since the pack
produces a character with the given code point rather than an integer
directly.)

Cheers,
Philip
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.