cvs commit: perlfaq perlfaq4.pod

[email protected] (Robert Spier) 24 Aug 2003 05:35:31 -0000
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
cvsuser     03/08/23 22:35:31

  Modified:    .        perlfaq4.pod
  Log:
  From: Iain Truskett <[email protected]>
  To: [email protected]
  Subject: perlfaq4 - tweaks for "How do I convert between numeric representation\
  s"
  Date: Sat, 23 Aug 2003 23:33:10 +1000
  
  [1  <text/plain; us-ascii (7bit)>]
  Attached are some diffs that accomplish the following things for
  the radix conversion.
  
  1. Some common synonyms added to the heading (for keyword searches)
  
  2. Consistent use of ':' on the 'Using...' parts.
  
  3. Useless use of sprintf removed.
  
  4. Mention both %X and %x for dec->hex.
  
  5. Mention ord's ability to convert binary numbers.
  
  6. Mention sprintf's %b on 5.6+.
  
  Revision  Changes    Path
  1.46      +24 -19    perlfaq/perlfaq4.pod
  
  Index: perlfaq4.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -w -r1.45 -r1.46
  --- perlfaq4.pod	23 Aug 2003 06:08:43 -0000	1.45
  +++ perlfaq4.pod	24 Aug 2003 05:35:31 -0000	1.46
  @@ -1,6 +1,6 @@
   =head1 NAME
   
  -perlfaq4 - Data Manipulation ($Revision: 1.45 $, $Date: 2003/08/23 06:08:43 $)
  +perlfaq4 - Data Manipulation ($Revision: 1.46 $, $Date: 2003/08/24 05:35:31 $)
   
   =head1 DESCRIPTION
   
  @@ -102,7 +102,7 @@
   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
  @@ -121,18 +121,15 @@
   
   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:
   
  @@ -144,13 +141,14 @@
   
   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);
  @@ -167,13 +165,11 @@
   
   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:
   
  @@ -188,7 +184,7 @@
   
       $oct = sprintf("%o", 3735928559);
   
  -Using Bit::Vector
  +Using Bit::Vector:
   
       use Bit::Vector;
       $vec = Bit::Vector->new_Dec(32, -559038737);
  @@ -201,11 +197,16 @@
   
   	$number = 0b10110110;
   
  -Using pack and ord
  +Using ord:
  +
  +    my $input = "10110110";
  +    $decimal = ord( "0b$input" );
  +
  +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)));
  @@ -219,6 +220,10 @@
       $dec = $vec->to_Dec();
   
   =item How do I convert from decimal to binary
  +
  +Using sprintf (perl 5.6+):
  +
  +    $bin = sprintf("%b", 3735928559);
   
   Using unpack;