For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > FAQ 4.5 How do I convert between numeric representations/bases/radixes?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author FAQ 4.5 How do I convert between numeric representations/bases/radixes?
PerlFAQ Server

2005-07-26, 5:02 pm

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.

--------------------------------------------------------------------

4.5: 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 between number
representations. This is intended to be representational rather than
exhaustive.

Some of the examples below use the Bit::Vector module from CPAN. The
reason you might choose Bit::Vector over the perl built in functions is
that it works with numbers of ANY size, that it is optimized for speed
on some operations, and for at least some programmers the notation might
be familiar.

How do I convert hexadecimal into decimal
Using perl's built in conversion of 0x notation:

$dec = 0xDEADBEEF;

Using the hex function:

$dec = hex("DEADBEEF");

Using pack:

$dec = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8)));

Using the CPAN module Bit::Vector:

use Bit::Vector;
$vec = Bit::Vector->new_Hex(32, "DEADBEEF");
$dec = $vec->to_Dec();

How do I convert from decimal to hexadecimal
Using sprintf:

$hex = sprintf("%X", 3735928559); # upper case A-F
$hex = sprintf("%x", 3735928559); # lower case a-f

Using unpack:

$hex = unpack("H*", pack("N", 3735928559));

Using Bit::Vector:

use Bit::Vector;
$vec = Bit::Vector->new_Dec(32, -559038737);
$hex = $vec->to_Hex();

And Bit::Vector supports odd bit counts:

use Bit::Vector;
$vec = Bit::Vector->new_Dec(33, 3735928559);
$vec->Resize(32); # suppress leading 0 if unwanted
$hex = $vec->to_Hex();

How do I convert from octal to decimal
Using Perl's built in conversion of numbers with leading zeros:

$dec = 033653337357; # note the leading 0!

Using the oct function:

$dec = oct("33653337357");

Using Bit::Vector:

use Bit::Vector;
$vec = Bit::Vector->new(32);
$vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
$dec = $vec->to_Dec();

How do I convert from decimal to octal
Using sprintf:

$oct = sprintf("%o", 3735928559);

Using Bit::Vector:

use Bit::Vector;
$vec = Bit::Vector->new_Dec(32, -559038737);
$oct = reverse join('', $vec->Chunk_List_Read(3));

How do I convert from binary to decimal
Perl 5.6 lets you write binary numbers directly with the 0b
notation:

$number = 0b10110110;

Using oct:

my $input = "10110110";
$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);

# substr() is used to left pad a 32 character string with zeros.

Using Bit::Vector:

$vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
$dec = $vec->to_Dec();

How do I convert from decimal to binary
Using sprintf (perl 5.6+):

$bin = sprintf("%b", 3735928559);

Using unpack:

$bin = unpack("B*", pack("N", 3735928559));

Using Bit::Vector:

use Bit::Vector;
$vec = Bit::Vector->new_Dec(32, -559038737);
$bin = $vec->to_Bin();

The remaining transformations (e.g. hex -> oct, bin -> hex, etc.)
are left as an exercise to the inclined reader.



--------------------------------------------------------------------

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

AUTHOR AND COPYRIGHT

Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com