ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы.



2.4. Converting Between Binary and Decimal

Problem

You have an integer whose binary representation you'd like to print out, or a binary representation that you'd like to convert into an integer. You might want to do this if you were displaying non-textual data, such as what you get from interacting with certain system programs and functions.

Solution

To convert a Perl integer to a text string of ones and zeros, first pack the integer into a number in network byte order[1] (the "N" format), then unpack it again bit by bit (the "B32" format).

[1] Also known as big-endian, or MSB (Most-Significant Bit first) order.

sub dec2bin {
    my $str = unpack("B32", pack("N", shift));
    $str =~ s/^0+(?=\d)//;   # otherwise you'll get leading zeros
    return $str;
}

To convert a text string of ones and zeros to a Perl integer, first massage the string by padding it with the right number of zeros, then just reverse the previous procedure.

sub bin2dec {
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}

Discussion

We're talking about converting between strings like "00100011" and numbers like 35. The string is the binary representation of the number. We can't solve either problem with sprintf (which doesn't have a "print this in binary" format), so we have to resort to Perl's pack and unpack functions for manipulating strings of data.

The pack and unpack functions act on strings. You can treat the string as a series of bits, bytes, integers, long integers, floating-point numbers in IEEE representation, checksums  - among other strange things. The pack and unpack functions both take formats, like sprintf, specifying what they should do with their arguments.

We use pack and unpack in two ways: "treat this string as a series of bits" and "treat this string as containing a binary representation of an integer." When we treat the string as a series of bits, we have to understand how pack will behave. Such a string is treated as a series of bytes, a byte being eight bits. The bytes are always counted from left to right (the first eight bits are the first byte, the next eight bits are the second, and so on), but the bits within each byte can be counted left-to-right as well as right-to-left.

We use pack with a template of "B" to work with bits within each byte from left to right. This is the order that the "N" format expects them in, which we use to treat the series of bits as representing a 32-bit integer.

$num = bin2dec('0110110');  # $num is 54
$binstr = dec2bin(54);      # $binstr is 110110

See Also

The pack and unpack functions in perlfunc (1) and Chapter 3 of Programming Perl; we also use pack and unpack in Recipe 1.4; to convert between decimal, hexadecimal, and octal, see Recipe 2.16


Previous: 2.3. Rounding Floating-Point NumbersPerl CookbookNext: 2.5. Operating on a Series of Integers
2.3. Rounding Floating-Point NumbersBook Index2.5. Operating on a Series of Integers