将C#转换为php?

I'm trying to convert this function from C# to php, but how do I deal with needing to type cast to a byte data type (up to 0xFF) when php only deals with integers?

public static byte[] CreateKeyBlock()
{
    byte[] ac = new byte[6];
    for (int j = 0; j < 6; j++)
    {
        ac[j] = (byte)Rand.Next(0, 255);
        int i;
        byte d;

        ac[3] = (byte)(ac[1] ^ ac[2] ^ ac[4]);
        ac[0] = (byte)((ac[4] ^ ac[1]) + ac[2]);
        ac[5] = 0;
        d = (byte)0xd5;

        for (i = 0; i < 102; i++)
        {
            if (i % 5 != 0)
            {
                ac[(i % 6)] = (byte)(ac[(i % 6)] + d);
                d = ac[(i % 6)];
            }
            else
            {
                ac[(i % 6)] = (byte)(ac[(i % 6)] ^ d);
                d = ac[(i % 6)];
            }
        }
    }

    return ac;
}
function CreateKeyBlock()
{
    $ac = '';

    for ($j = 0; $j < 6; $j++)
    {
        $ac{$j} = rand(0, 255);

        $ac{3} = ($ac{1} ^ $ac{2} ^ $ac{4}) & 0xff;
        $ac{0} = (($ac{4} ^ $ac{1}) + $ac{2}) & 0xff;
        $ac{5} = 0;

        $d = 0xd5;

        for ($i = 0; $i < 102; $i++)
        {
            if ($i % 5 != 0)
            {
                $ac{($i % 6)} = ($ac{($i % 6)} + $d) & 0xff;
                $d = $ac{($i % 6)};
            }
            else
            {
                $ac{($i % 6)} = ($ac{($i % 6)} ^ $d) & 0xff;
                $d = $ac{($i % 6)};
            }
        }
    }
    return $ac;
}

You don't need to cast anything. Just make sure that any operations that might cause values to get above 255 get their results clamped to the byte range, e.g. by using $value & 0xff instead of just $value.

It's also worthwhile to consider in what form to return the result -- PHP is not C#, so a string might make more sense here.

Try this code:

function CheckByte( $value ) {
    return $value & 0xff;
}

function CreateKeyBlock() {
    $ac = array(0, 0, 0, 0, 0, 0);

    for ( $j = 0; $j < 6; $j++ ) {
        $ac[$j] = rand( 0, 255 );

        $ac[3] = CheckByte( $ac[1] ^ $ac[2] ^ $ac[4] );
        $ac[0] = CheckByte( ( $ac[4] ^ $ac[1] ) + $ac[2] );
        $ac[5] = 0;
        $d = hexdec( "0xd5" );

        for ( $i = 0; $i < 102; $i++ ) {
            $ac[$i % 6] = CheckByte( $i % 5 != 0 ? $ac[$i % 6] + $d : $ac[$i % 6] ^ $d );
            $d = $ac[$i % 6];
        }
    }
    return $ac;
}