- 论坛徽章:
- 0
|
呵呵 也难怪老大要发火,perl文档里面写得清清楚楚,而且给出来了N种解决方案。。。。
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(); |
|