...

View Full Version : Binary unsigned short conversion



Afrow UK
03-10-2007, 07:55 PM
I'm using unpack to process a reply from the Goldsource (Half-Life) master servers. The reply is a list a 6 byte header followed by IP addresses made up of 6 bytes each.

The reply is in the format:
byte.byte.byte.byte:unsigned short

The unsigned short is network ordered, or supposedly big endian.
More information here: http://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol

I'm using this to unpack it and display the IP addresses:

$outData = unpack("C*/C*/C*/C*/n*", $out);
for ($i = 1; $i<=sizeof($outData); $i+=6)
echo $outData[$i] . "." . $outData[$i+1] . "." . $outData[$i+2] . "." . $outData[$i+3] . ":" . $outData[$i+4] . "<br />";

The IP address itself is displayed correctly, but the port number is always 105! Any ideas why?

Stu

Afrow UK
03-10-2007, 08:27 PM
Never mind,



for ($i=0; $i<strlen($out); $i+=6)
{
$outData = unpack("@" . $i . "/C4ip/n1port", $out);
echo $outData['ip1'] . "." . $outData['ip2'] . "." . $outData['ip3'] . "." . $outData['ip4'] . ":" . $outData['port'] . "<br />";
}


Stu



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum