diff options
author | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2003-09-20 10:00:12 +0000 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2003-09-20 10:00:12 +0000 |
commit | 6f0efb172c82e82c39073291db2462e39dbe4104 (patch) | |
tree | a946f1097d8611ba451bc068efe853e50ba5c7e4 /pod/perlfaq4.pod | |
parent | fb9cc17475f7385a07b3c8693a1ca73c68a368d6 (diff) | |
download | perl-6f0efb172c82e82c39073291db2462e39dbe4104.tar.gz |
FAQ sync.
p4raw-id: //depot/perl@21289
Diffstat (limited to 'pod/perlfaq4.pod')
-rw-r--r-- | pod/perlfaq4.pod | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index 70af877062..53c7d02194 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq4 - Data Manipulation ($Revision: 1.44 $, $Date: 2003/07/28 17:35:21 $) +perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date: 2003/09/20 06:37:43 $) =head1 DESCRIPTION @@ -120,7 +120,7 @@ Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed. -=head2 How do I convert between numeric representations? +=head2 How do I convert between numeric representations/bases/radixes? As always with Perl there is more than one way to do it. Below are a few examples of approaches to making common conversions @@ -139,18 +139,15 @@ programmers the notation might be familiar. Using perl's built in conversion of 0x notation: - $int = 0xDEADBEEF; - $dec = sprintf("%d", $int); + $dec = 0xDEADBEEF; Using the hex function: - $int = hex("DEADBEEF"); - $dec = sprintf("%d", $int); + $dec = hex("DEADBEEF"); Using pack: - $int = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8))); - $dec = sprintf("%d", $int); + $dec = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8))); Using the CPAN module Bit::Vector: @@ -162,13 +159,14 @@ Using the CPAN module Bit::Vector: Using sprintf: - $hex = sprintf("%X", 3735928559); + $hex = sprintf("%X", 3735928559); # upper case A-F + $hex = sprintf("%x", 3735928559); # lower case a-f -Using unpack +Using unpack: $hex = unpack("H*", pack("N", 3735928559)); -Using Bit::Vector +Using Bit::Vector: use Bit::Vector; $vec = Bit::Vector->new_Dec(32, -559038737); @@ -185,13 +183,11 @@ And Bit::Vector supports odd bit counts: Using Perl's built in conversion of numbers with leading zeros: - $int = 033653337357; # note the leading 0! - $dec = sprintf("%d", $int); + $dec = 033653337357; # note the leading 0! Using the oct function: - $int = oct("33653337357"); - $dec = sprintf("%d", $int); + $dec = oct("33653337357"); Using Bit::Vector: @@ -206,7 +202,7 @@ Using sprintf: $oct = sprintf("%o", 3735928559); -Using Bit::Vector +Using Bit::Vector: use Bit::Vector; $vec = Bit::Vector->new_Dec(32, -559038737); @@ -217,13 +213,18 @@ Using Bit::Vector Perl 5.6 lets you write binary numbers directly with the 0b notation: - $number = 0b10110110; + $number = 0b10110110; + +Using oct: + + my $input = "10110110"; + $decimal = oct( "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))); |