Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

parse string and remove nonprintable char

Back to Forum: Tech Off
  • User profile image
    edman747

    Hello,

    I have a function that receives a udp message as an array of bytes, converts it to a string. What I'm trying to figure out is how to split the string and remove the nonprintable chars. In the following code snipet $d[3] looks like: "[2120] sc_reflux svencoop4 Sven"

    Whatever is before and after the word sc_reflux is not a space. Any help? TIA

     

    function hlrcv
    {

    # set port to the value of the local end point port ($leep) that was used during the send.

    $port = $lepp
    $endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Loopback,$port)
    echo "Listening for response on: $endpoint"
    $udpclient = new-Object System.Net.Sockets.UdpClient $port
    # response in a byte array.
    $content = $udpclient.Receive([ref]$endpoint)

    # make it a string and find the map name.
    $c = [Text.Encoding]::ASCII.GetString($content)
    $d = $c.split(" ")
    $d[3]
    $e = $d[3] | select-string "svencoop4" | % { $_ -replace "svencoop4","" }
    $f = $e.split("`]") | select-string "Sven" | % { $_ -replace "Sven","" }
    echo "The map name is [$f], without the [] and the nonprintable chars"
    $udpclient.Close()
    }

     

    Output:

    Listening for response on: 127.0.0.1:51843
    [2120] sc_reflux svencoop4 Sven

    The map name is [ sc_reflux  ], without the [] and the nonprintable chars

     

    What is desired:

    map sc_reflux

  • User profile image
    W3bbo

    Use the System.Char.IsControl(Char c) predicate.

  • User profile image
    ManipUni

    If it is an ASCII string (as it appears) then just cast it to a number and check if it is between 32 and 126.

     

     

    foreach (char buff in textBox1.Text.ToCharArray())
     {
      if((int)buff >= 32 && (int)buff <= 126)
       {
       }
     }

     

  • User profile image
    edman747

    ManipUni said:

    If it is an ASCII string (as it appears) then just cast it to a number and check if it is between 32 and 126.

     

     

    foreach (char buff in textBox1.Text.ToCharArray())
     {
      if((int)buff >= 32 && (int)buff <= 126)
       {
       }
     }

     

    It appears that the junk before and after the map name(sc_reflux) is a 0x00

    I got this info from: http://developer.valvesoftware.com/wiki/Server_queries

     

    I still cannot figure out how to remove the extra junk.

     

     

  • User profile image
    edman747

    edman747 said:
    ManipUni said:
    *snip*

    It appears that the junk before and after the map name(sc_reflux) is a 0x00

    I got this info from: http://developer.valvesoftware.com/wiki/Server_queries

     

    I still cannot figure out how to remove the extra junk.

     

     

    Solution:

    $c is an ASCII string from the array of bytes received ($content).

    split $c on the 0x00 then the map name is in $d[1]

     

    function hlrcv
    {

    # set port to the value of the local end point port ($leep) that was used during the send.

    $port = $lepp
    $endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Loopback,$port)
    echo "Listening for response on: $endpoint"
    $udpclient = new-Object System.Net.Sockets.UdpClient $port

    # response in a byte array.
    $content = $udpclient.Receive([ref]$endpoint)

    #make it a string and find the map name.
    $c = [Text.Encoding]::ASCII.GetString($content)
    $d = $c.split([char][byte]"0x00")

    $f = $d[1]

    echo "The map name is [$f]"
    $udpclient.Close()
    }

  • User profile image
    Sven Groot

    edman747 said:
    ManipUni said:
    *snip*

    It appears that the junk before and after the map name(sc_reflux) is a 0x00

    I got this info from: http://developer.valvesoftware.com/wiki/Server_queries

     

    I still cannot figure out how to remove the extra junk.

     

     

    It seems that what the server returns is actually a list of null-terminated utf-8 strings. Dealing with this is pretty simple.

    $response = $udpClient.Receive([ref]$endpoint)
    $responseString = [System.Text.Encoding]::UTF8.GetString($response)
    $parts = $responseString.Split("`0") # Split on the null character

     

    This will give you an array with the individual strings, one of which will be your map name.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.