How do I convert from binary to decimal

Perl 5.6 lets you write binary numbers directly with the 0b notation:

	$number = 0b10110110;
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();


Back to perlfaq4