Re: [PHP4BETA] bugs and a leak
| From: | Rasmus Lerdorf | Date: | Thu, 13 Apr 2000 15:34:13 +0000 |
| Subject: | Re: [PHP4BETA] bugs and a leak | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-13502@lists.php.net to get a copy of this message | ||
> I'm not sure if these are bugs or if I'm just expecting too much... Using
> RC1.
>
> First problem I've noticed is that fread($fp, -128) does not move the
> offset at all, much less to 128 bytes from the end... Shouldn't this work?
I think you are confusing fread() and fseek() here.
> Second, there seems to be inconsistency in the way php deals with
> hex. For example, php does not consider 0xff and ff to be equal. Shouldn't
> PHP ignore the 0x when doing a comparison? I noticed this reading a byte
> from a file which had the value 0xff, however php stores it as just ff and
> doing a comparison between the read byte and 0xff failed when they
> should both have the same value. In other words, if the first byte of a
> file is 11111111 and I do
>
> $fp = fopen($file, "r");
> $byte = bin2hex(fread($fp, 1));
> $byte == 0xff ? print "Yep" : print "Nope";
>
> This will output Nope even though they are the same value.
Huh? If the first byte of the file is 11111111 (in binary, I
presume) then doing an fread($fp,1) is going to return 255. You then pass
effectively "255" to bin2hex() even though 255 is definitely not a binary
value. I think you meant to use dechex() there. However, dechex() simply
returns a string.
> Also, if $byte is read from a file to and converted to hex for the value
> ff, the following two things do not do the same thing
>
> $byte &= f0;
> $byte &= 0xf0;
>
> The first one, while giving the correct answer, issues the following
You are confusing a bunch of things here. A number is just a
number. There is no such thing as a hex number or a binary number if you
think about it. In your case you are talking about the number 255
(decimal) which happens to be 0xff in hex or 11111111 in binary. You do
not convert a number between formats when working with it internall. By
that I mean that it is comletely wrong to "convert" $byte to hex just
because you want to compate it to another hex number. dechex() returns a
string representation of the number, it does not return the number itself
since there would be no point to that.
I am not sure I am explaining this very well. How about an example:
$a = 255;
$b = 0xff;
if($a==$b) echo "They are equal";
This prints "They are equal";
-Rasmus