PDA

View Full Version : How can I read the Cars-Bytes


HorsePower
5th November 2006, 10:17
I've searched this forum about this, but didn't find anything.

I'm writing some PHP stuff, trying to read all the information I can out of the host list. Everything works fine, but I have a problem, understanding the 4 bytes of Cars-Ulong variable.

This peace of code gets me the 4 bytes of the car information of a server:

$tmp = "";
for ($w=0; $w<4; $w++) $tmp .= $this->HostList[$x++];
Here, the host list has been retrieved in the usual way from LFSW and is stored in a huge string variable.

Now I have a string of 4 letters in $tmp. How can I convert this into a ULONG number?

I suppose, if I have the correct value as unsigned long int, I can convert it to a binary string by using

$carsbinarystring = decbin(carsulongvalue);
Then I can parse the string for 0's and 1's to check, which cars are enabled on that server.

I would appreciate any help! :nod:

Thanks in advance,

HorsePower

HorsePower
5th November 2006, 11:02
I think I found the solution by myself. Trial and error helped me. :D

This is how I do it (the function argument $cars is equal to the string $tmp from the 1st post):


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

$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->".