Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何使用可变长度字符串选择字节数组的元素_Powershell - Fatal编程技术网

Powershell 如何使用可变长度字符串选择字节数组的元素

Powershell 如何使用可变长度字符串选择字节数组的元素,powershell,Powershell,试图解码UDP消息时,它是一个128字节的数组,第一个可变长度字符串从第7个字节开始。我想做的是将数组分割成单独的元素,并根据typebyte、string对每个元素进行解码。此处定义了数据格式/类型: 这是原始数据。 接收缓冲区:255 255 255 255 255 73 48 115 99 52 46 120 32 84 101 115 116 32 83 101 114 118 101 114 91 50 56 49 51 93 0 99 48 97 48 101 0 115 118 1

试图解码UDP消息时,它是一个128字节的数组,第一个可变长度字符串从第7个字节开始。我想做的是将数组分割成单独的元素,并根据typebyte、string对每个元素进行解码。此处定义了数据格式/类型:

这是原始数据。 接收缓冲区:255 255 255 255 255 73 48 115 99 52 46 120 32 84 101 115 116 32 83 101 114 118 101 114 91 50 56 49 51 93 0 99 48 97 48 101 0 115 118 101 110 99 111 112 5 2 0 83 118 101 110 32 67 111 45 111 112 32 52 46 54 32 111 114 32 108 97 116 101 114 0 70 0 0 12 0 100 119 0 0 49 46 49 46 50 46 49 0 145 145 105 3 44 67 175 180 9 64 1 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

下面是在0x00上拆分的字符串数组。 字符串响应:???I0sc4.x测试服务器[2813]c0a0e svencoop4 Sven Co OP4.6或更高版本F♀ dw 1.1.2.1??i♥,C@☺F

这可以精确到单词后面的第五个元素F。在那之后事情变得有点混乱,我不知道如何选择/解码剩余的元素

# convert to string.
$StringResponse = [Text.Encoding]::ASCII.GetString($ReceiveBuffer)  
# make an array of strings.  
$SplitString = $StringResponse.split([char][byte]"0x00")  
# store the value of individual elements.  
$servername_split =             $SplitString[0].split([char]"0")  
             $Map =             $SplitString[1]  
   $gamedirectory =             $SplitString[2]  
 $gamedescription =             $SplitString[3]  
           $appid = [byte][char]$splitstring[4] 
使用原始数据: 这用于访问四字节头255、255、255、255之后的前两个字节元素。然后可变长度字符串开始

                  $type = [char]$ReceiveBuffer[4]  
$NetworkProtocolVersion =       $ReceiveBuffer[5]  
所以我不能这样做来访问原始数据末尾附近的字节元素,除非我事先知道可变长度字符串数据使用了多少字节

   $numplayers =       $ReceiveBuffer[72]  
$maxnumplayers =       $ReceiveBuffer[73]  
      $numbots =       $ReceiveBuffer[74]  
    $dedicated = [char]$ReceiveBuffer[75]  
           $os = [char]$ReceiveBuffer[76]  
  $passwordreq =       $ReceiveBuffer[77]  
       $secure =       $ReceiveBuffer[78]  
控制台输出:

Type: I
Network Protocol Version: 48
Server Name: sc4.x Test Server[2813]
Map: c0a0e
Game Directory: svencoop4
Game Description: Sven Co-op 4.6 or later
AppID: 70

蒂娅,

嘿,我想我明白了

# Receive Buffer size varies from server to server.  
$ReceiveBuffer = New-Object  Byte[] 128

# Info Reply  
$BytesReceived = 0  
Try  
{  
    $BytesReceived = $UdpSocket.Receivefrom($ReceiveBuffer, [REF]$UdpSocket.LocalEndpoint)  
}  

# An Exception will be thrown if the server is down or hung.  
Catch [Net.Sockets.SocketException]  
{  
    # Set Flag  
    $Script:NoConnectionTimeOut = $False  
}  

# Verify and Decode HLDS server response.  
If ($BytesReceived -gt 0)  
{  
    # Ignore the four byte header and store the values of the first two bytes.  
    $type =       [char]$ReceiveBuffer[4]  
    $netversion =       $ReceiveBuffer[5]  

    # Loop through the byte array and store each type of data.  
    $i = 0  
    $StringNum = 0  
    $FoundAppID = $False  
    foreach ($element in $receivebuffer)  
    {  
        If ($i -gt 5 -and $i -lt 80)
        {
            If ($element -ne 0)
            {
                $string = $string + [Text.Encoding]::ASCII.GetString($element)
            }
            Else
            {
                $StringNum++
                if ($stringnum -eq 1) {      $ServerName = $string }
                if ($stringnum -eq 2) {         $MapName = $string }
                if ($stringnum -eq 3) {     $InfoGameDir = $string }
                if ($stringnum -eq 4) { $GameDescription = $string }
                $string = ""
            }
        }
        if ($i -gt 50 -and $element -eq 70 -and $FoundAppID -eq $False)
        {
            if ($element -eq 70)
            {
                $FoundAppID = $True
                $AppID = $element;$arrayIndex = $i
                $i++;$i++
                $numplayers =      $ReceiveBuffer[$i]
                $i++
                $maxnumplayers =   $ReceiveBuffer[$i]
                $i++
                $numbots =         $ReceiveBuffer[$i]
                $i++
                $dedicated = [char]$ReceiveBuffer[$i]
                $i++
                $os =        [char]$ReceiveBuffer[$i]
                $i++
                $Password =        $ReceiveBuffer[$i]
                $i++
                $Secure =          $ReceiveBuffer[$i]
                for ($j = 1; $j -lt 9; $j++)
                {
                    $i++
                    $GameVersion = $GameVersion + [Text.Encoding]::ASCII.GetString($ReceiveBuffer[$i])
                }
                $i++
                $EDF =             $ReceiveBuffer[$i]
            }
        }
        $i++
    }

    # Write the server info.
    write-host "`n                    Type: $type"
    write-host "        Protocol Version: $netversion"
    write-host "             Server Name: $ServerName"
    write-host "                     Map: $MapName"
    write-host "          Game Directory: $InfoGameDir"
    write-host "        Game Description: $GameDescription"
    write-host "                   AppID: $AppID, $arrayIndex"
    write-host "       Number of players: $numplayers"
    write-host "         Maximum players: $maxnumplayers"
    write-host "          Number of bots: $numbots"
    write-host "               Dedicated: $dedicated"
    write-host "                      OS: $os"
    write-host "                Password: $Password"
    write-host "                  Secure: $Secure"
    write-host "            Game Version: $GameVersion"
    write-host "                     EDF: $EDF"
    #"Raw Data: $ReceiveBuffer"