2011年11月14日星期一

Perl : 10进制转换成16进制

#!/usr/bin/perl
use strict;
use warnings;
use Math::BigInt;

#需求:将一个10进制的大数,用16进制分解成4

 

#1:类似C的写法

my $l = 281474983985353;

my $a = $l % 0x100000000;

my $b = ($l-$a)/0x100000000;

my $major = $b >> 16;

my $minor = $b % 0x10000;

my $build = $a >> 16;

my $ext = $a % 0x10000;

print "$major $minor $build $ext\n";

 

#2:用Math::BigInt大数库

my $t = new Math::BigInt('281474983985353');

my @result;

while($t){

         my ($t, $s) =$t->bdiv(0x10000);

         unshift @result, s;

}

print join(" ", @result, "\n");

 

#3:可以用pack,需要支持Q选项,与perl编译的版本有关

没有评论:

发表评论