14

Is there any possibility to see binary representation of variable?

PaulP
  • 1,925
  • 2
  • 20
  • 25

7 Answers7

10

Like so:

echo decbin(3); // 11
Josh
  • 12,448
  • 10
  • 74
  • 118
8

Another solution:

function d2b($dec, $n = 16) {
    return str_pad(decbin($dec), $n, "0", STR_PAD_LEFT);
}

Example:

// example:
echo d2b(E_ALL);
echo d2b(E_ALL | E_STRICT);
echo d2b(0xAA55);
echo d2b(5);

Output:
0111011111111111
0111111111111111
1010101001010101
0000000000000101
Mario Campa
  • 4,092
  • 1
  • 26
  • 25
5

decbin(your_int) will return a string in binary numbers representing the same value as your_int, assuming that's what you're asking for.

Wooble
  • 87,717
  • 12
  • 108
  • 131
3
<?php
/**
 *    Returns an ASCII string containing
 *    the binary representation of the input data .
**/
function str2bin($str, $mode=0) {
    $out = false;
    for($a=0; $a < strlen($str); $a++) {
        $dec = ord(substr($str,$a,1));
        $bin = '';
        for($i=7; $i>=0; $i--) {
            if ( $dec >= pow(2, $i) ) {
                $bin .= "1";
                $dec -= pow(2, $i);
            } else {
                $bin .= "0";
            }
        }
        /* Default-mode */
        if ( $mode == 0 ) $out .= $bin;
        /* Human-mode (easy to read) */
        if ( $mode == 1 ) $out .= $bin . " ";
        /* Array-mode (easy to use) */
        if ( $mode == 2 ) $out[$a] = $bin;
    }
    return $out;
}
?>

Copied from: http://php.net/manual/en/ref.strings.php

tim_a
  • 940
  • 1
  • 7
  • 20
  • it'd be easier to replace the inner loop with decbin(), or at least use bit shifting rather than exponential functions. – Marc B Apr 18 '11 at 15:46
  • yes I also noticed it. It's not mine code I just copied from the php site. – tim_a Apr 18 '11 at 16:05
3

Or you may use base_convert function to convert symbol codes to binary, here's a modified function:

function str2bin($str) 
{
    $out=false; 
    for($a=0; $a < strlen($str); $a++)
    {
        $dec = ord(substr($str,$a,1)); //determine symbol ASCII-code
        $bin = sprintf('%08d', base_convert($dec, 10, 2)); //convert to binary representation and add leading zeros
        $out .= $bin;
    }
    return $out;
}

It is useful to convert inet_pton() result to compare ipv6 addresses in binary format (since you cannot really convert a 128-bit ipv6 address to integer, which is 32- or 64-bit in php). You can find more on ipv6 and php here (working-with-ipv6-addresses-in-php) and here (how-to-convert-ipv6-from-binary-for-storage-in-mysql).

Community
  • 1
  • 1
Snifff
  • 1,784
  • 2
  • 16
  • 28
2
$a = 42;
for($i = 8 * PHP_INT_SIZE - 1; $i >= 0; $i --) {
    echo ($a >> $i) & 1 ? '1' : '0';
}
dev1
  • 21
  • 1
  • 4
1

What about: <?php $binary = (binary) $string; $binary = b"binary string"; ?>

(from php.net)

Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • That doesn't show the string in binary representation, it only "converts" (or casts) a literal string into a binary string. – Pedro Cunha Apr 19 '11 at 09:47