Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    UK
    Posts
    177
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Binary unsigned short conversion

    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/w...Query_Protocol

    I'm using this to unpack it and display the IP addresses:
    Code:
    $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
    if ($ENV{'QUERY_STRING'} eq "Afrow UK") {
    print "$ENV{'QUERY_STRING'} rocks!";
    } else {
    print qq~$ENV{'QUERY_STRING'} sucks :)~;
    }

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Location
    UK
    Posts
    177
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Never mind,

    Code:
      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
    if ($ENV{'QUERY_STRING'} eq "Afrow UK") {
    print "$ENV{'QUERY_STRING'} rocks!";
    } else {
    print qq~$ENV{'QUERY_STRING'} sucks :)~;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •