PHP strings essentially are byte arrays. The fastest way would simply be:
for ($i = 0, $length = strlen($data); $i < $length; $i++) {
$byte = $data[$i];
...
}
If you manipulate the byte with bitwise operators, you'll hardly get more efficient than that. You can write the byte back into the string with $data[$i] = $newValue
. If you need to turn a single byte into an int, use ord()
, the other way around use chr()
.