Re: Byte conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 6/25/07, Stut <stuttle@xxxxxxxxx> wrote:
Eric Butera wrote:
> I've been trying to figure out a way to do this all day and I'm afraid
> I might need a bit of help.  Basically I am trying to port over
> something from Java to PHP and I'm stuck on one particular piece of
> code:
>
> if ((ba[i + 0] == (byte)0xa7) && (ba[i + 1] == (byte)0x51)) {
>
> The code is looping through a byte array and checking if the current
> value matches "(byte)0xa7", etc.  If there is a match, then it breaks
> and returns the value of i.  There are quite a few more values than
> this in the conditional, but I figure this is enough to get the point
> across.  My problem is I'm not exactly sure what the value of
> (byte)0xa7 is.
>
> I tried
> $buffer = file_get_contents('Dining_Room.rti');
> $count = strlen($buffer);
> for ($x=0; $x < $count; $x++) {
>     $char = substr($buffer, $x, 1);
>     echo $char;
>     if ($char == 0xa7 && $char == 0x51) {

This is *never* going to be true, mainly because $char cannot be equal
to both 0xA7 *and* 0x51 at the same time.

>        return $x;
>     }
> }
>
> but it never matches.  Any pointers?

Yeah, understand the code before attempting to port it. The if you
posted at the top is trying to find 0xA7 followed by 0x51. Your ported
code is not.

Try this...

$buffer = file_get_contents('Dining_Room.rti');
$count = strlen($buffer);
for ($x=0; $x < $count; $x++) {
     $char1 = substr($buffer, $x, 1);
     $char2 = substr($buffer, $x+1, 1);
     echo $char1;
     if ($char1 == 0xa7 && $char2 == 0x51) {
        return $x;
     }
}

-Stut

--
http://stut.net/


Oops!  I'm actually not doing that exactly.  I just wanted a simple
example to get the point across.  I should have checked my work twice.
The main goals of my post were to find out if a literal 0xa7 is ok to
compare with, and if that value would be the equivalent the Java
typecast (byte)0xa7.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux