Jon wrote:
Seriously guys... am I completely missing something important here? An empty array evaluates as false. The null character evaluates as false. The STRING "0" even evaluates as false yet a binary zero does not?

Even better yet bindec() doesn't appear to be binary safe so it will work for a string representation of binary data where the 0's and 1's are ascii characters but pass it an actual binary string and it always returns 0.

(sigh)

Someone help me understand this.  Isn't this a bug or oversight?

The character '0' (zero) is not the same as a byte with all bits turned off.

   (char) '0' = (binary) '00110000' = ord('0') = (int) 48

'bindec' returns the decimal equivalent of the binary number represented by the binary_string argument. Note the BINARY STRING key word. I'm guessing a Binary String is a string consisting of the characters '0' or '1' not bits. Each character is 8 bits, so a binary string is a sequence of the characters #48 and #49 from the ASCII table.

The 'character' ZERO is written with bits 00110000 in binary. This binary could be interpreted as either a character or an integer. If the binary is read as an integer, you'll get INT 48. INT 48 is NOT FALSE. Casting from string to int to 'binary string' is done even when you might not want it. Are you sure you are evaluating your binary '00000000' properly as you claim? Send a sample code, otherwise, see the following example:

--------------------------------------------------
<pre>
<?php

$zero = (string)'0';
$one = (string)'1';
var_dump($zero);
var_dump($one);
printf("char: %s, ord: %d, byte: %08b\n", $zero, ord($zero), $zero);
printf("char: %s, ord: %d, byte: %08b\n", $one, ord($one), $one);

// string(1) "0"
// string(1) "1"
// char: 0, ord: 48, byte: 00000000
// char: 1, ord: 49, byte: 00000001

$zero = (int)'0';
$one = (int)'1';
var_dump($zero);
var_dump($one);
printf("char: %s, ord: %d, byte: %08b\n", $zero, ord($zero), $zero);
printf("char: %s, ord: %d, byte: %08b\n", $one, ord($one), $one);

// int(0)
// int(1)
// char: 0, ord: 48, byte: 00000000
// char: 1, ord: 49, byte: 00000001

$zero = unpack('C', (string)'0');
$zero = array_pop($zero);
$one = unpack('C', (string)'1');
$one = array_pop($one);
var_dump($zero);
var_dump($one);
printf("char: %s, ord: %d, byte: %08b\n", $zero, ord($zero), $zero);
printf("char: %s, ord: %d, byte: %08b\n", $one, ord($one), $one);

// int(48)
// int(49)
// char: 48, ord: 52, byte: 00110000
// char: 49, ord: 52, byte: 00110001

?>
</pre>
--------------------------------------------------

Dante

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to