I think I found the solution by myself. Trial and error helped me.
This is how I do it (the function argument $cars is equal to the string $tmp from the 1st post):
<?php
function decode_cars($cars)
{
$binarystring = "";
// turn the character to an int and this into a binary string
$tmp = decbin(ord($cars[3]));
// add zeros in front of the string until it has 8 digits (because max value of byte is 255, i.e. 11111111)
while(strlen($tmp) < 8 ) $tmp = "0".$tmp;
// the last byte will be the first 8 binary digits
$binarystring .= $tmp;
// and so on ...
$tmp = decbin(ord($cars[2]));
while(strlen($tmp) < 8 ) $tmp = "0".$tmp;
$binarystring .= $tmp;
$tmp = decbin(ord($cars[1]));
while(strlen($tmp) < 8 ) $tmp = "0".$tmp;
$binarystring .= $tmp;
$tmp = decbin(ord($cars[0]));
while(strlen($tmp) < 8 ) $tmp = "0".$tmp;
$binarystring .= $tmp;
// just for debugging ...
// echo "binary string = ".$binarystring."<br>";
$blen = strlen($binarystring); // length of the binary string
$clen = count($this->carlist); // number of cars in car list
$count = 0;
$carnumber = 0;
$allowedcars = array(); // init the array of allowed cars on the server
for($i=$blen-1; $i>=0; $i--){ // reverse parsing of the binary string
if ($binarystring[$i] == "1"){
$allowedcars[$count] = $this->carlist[$carnumber];
// just for debugging ...
// echo "car = ".$this->carlist[$carnumber]."<br>";
$count++;
}
$carnumber++;
}
// return the array of allowed cars
return $allowedcars;
}
?>
The car list is defined by
<?php
$this->carlist = array( "XFG",
"XRG",
"XRT",
"RB4",
"FXO",
"LX4",
"LX6",
"MRT",
"UF1",
"RAC",
"FZ5",
"FOX",
"XFR",
"UFR",
"FO8",
"FXR",
"XRR",
"FZR",
"BF1"
);
?>
Maybe this will help some of you guys, too!
Note that these variables and functions are part of a PHP class, that's why the pointer $this sometimes occur. In a standalone version just remove "$this->".