[格式]各种进制互相转换

There's more than one way to do it!

[格式]各种进制互相转换

帖子523066680 » 2015-12-26 10:32

暂时照搬,有空翻译

perldoc -q convert

perldoc -q convert: SELECT ALL
Found in C:\Perl\lib\pods\perlfaq4.pod
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 later in perlfaq4 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:

my $dec = 0xDEADBEEF;

Using the "hex" function:

my $dec = hex("DEADBEEF");

Using "pack":

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

Using the CPAN module "Bit::Vector":

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

How do I convert from decimal to hexadecimal
Using "sprintf":

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

Using "unpack":

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

Using Bit::Vector:

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

And Bit::Vector supports odd bit counts:

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

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

my $dec = 033653337357; # note the leading 0!

Using the "oct" function:

my $dec = oct("33653337357");

Using Bit::Vector:

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

How do I convert from decimal to octal
Using "sprintf":

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

Using Bit::Vector:

use Bit::Vector;
my $vec = Bit::Vector->new_Dec(32, -559038737);
my $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:

my $number = 0b10110110;

Using "oct":

my $input = "10110110";
my $decimal = oct( "0b$input" );

Using "pack" and "ord":

my $decimal = ord(pack('B8', '10110110'));

Using "pack" and "unpack" for larger strings:

my $int = unpack("N", pack("B32",
substr("0" x 32 . "11110101011011011111011101111", -32)));
my $dec = sprintf("%d", $int);

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

Using Bit::Vector:

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

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

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

Using "unpack":

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

Using Bit::Vector:

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

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

Found in C:\Perl\lib\pods\perlfaq8.pod
How can I convert my shell script to perl?
Learn Perl and rewrite it. Seriously, there's no simple converter.
Things that are awkward to do in the shell are easy to do in Perl, and
this very awkwardness is what would make a shell->perl converter nigh-on
impossible to write. By rewriting it, you'll think about what you're
really trying to do, and hopefully will escape the shell's pipeline
datastream paradigm, which while convenient for some matters, causes
many inefficiencies.
论坛已转移 Code-By.Org 群号 322023604
头像
523066680
版主
 
帖子: 1680
注册: 2012-03-06 15:08

回到 Perl

在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客

cron
Not able to open ./cache/data_global.php