B4R Question Nee help parsing a string...

Cableguy

Expert
Licensed User
Longtime User
Hi guys,

Working with bytes has never been my strongest.... nor my weakest.... it's just not "my thing"...
But since B4R deals with Strings as bytes, I am forced to work with them....

I need help parsing this string:
B4X:
 GET /set?WiFiNetworkSSID=Bbox-8655E4EE&pass=Cables HTTP/1.1
This is part of a Astream_NewData received message, and I am using EREL's "remote configuration" example as a base for my current endeavor...
As you can imagine, I need to retrieve the "WiFiNetworkSSID" and the "pass" values....
In the remote configuration example, the response looks more like "set/Bbox-8655E4EE/Cables" and he parses it using the following code:
B4X:
            Dim ssid = "", password  = "" As String
            Dim i1 As Int = 0
            Dim i2 As Int = 0
            For Each b1() As Byte In bc.Split(Buffer, " ")
                If i1 = 1 Then
                    For Each b2() As Byte In bc.Split(b1, "/")
                        Select i2
                            Case 2
                                ssid = bc.StringFromBytes(b2)
                            Case 3
                                password = bc.StringFromBytes(b2)
                        End Select
                        i2 = i2 + 1
                    Next
                   
               End If
                i1 = i1 + 1
            Next

Thanks in advance
 
Solution
In case no one else replies...
This is untested because I don't have B4R installed on this computer.

B4X:
            Dim ssid = "", password  = "" As String
            Dim i1 As Int = 0
            Dim i2 As Int = 0
            For Each b1() As Byte In bc.Split(Buffer, " ")
                If i1 = 1 Then
                    For Each b2() As Byte In bc.Split(b1, "&")
                        For Each b3() As Byte In bc.Split(b2, "=")
                            Select i2
                                Case 1
                                    ssid = bc.StringFromBytes(b3)
                                Case 2
                                    password = bc.StringFromBytes(b3)
                            End Select...

William Lancee

Well-Known Member
Licensed User
Longtime User
In case no one else replies...
This is untested because I don't have B4R installed on this computer.

B4X:
            Dim ssid = "", password  = "" As String
            Dim i1 As Int = 0
            Dim i2 As Int = 0
            For Each b1() As Byte In bc.Split(Buffer, " ")
                If i1 = 1 Then
                    For Each b2() As Byte In bc.Split(b1, "&")
                        For Each b3() As Byte In bc.Split(b2, "=")
                            Select i2
                                Case 1
                                    ssid = bc.StringFromBytes(b3)
                                Case 2
                                    password = bc.StringFromBytes(b3)
                            End Select
                            i2 = i2 + 1
                        Next
                    Next
               End If
                i1 = i1 + 1
            Next
 
Upvote 0
Solution

Cableguy

Expert
Licensed User
Longtime User
You had me going in the right direction.
Here's what I ended up with:
B4X:
    If bc.IndexOf(Buffer, "GET") <> -1 Then
       If bc.IndexOf(Buffer, "/set") <> -1 Then
           Log("we got credentials!!")
            Dim ssid = "", password  = "" As String
            Dim i1 As Int = 0
            Dim i2 As Int = 0
            For Each b1() As Byte In bc.Split(Buffer, " ")
                If i1 = 1 Then
                    For Each b2() As Byte In bc.Split(b1, "=")
                        Select i2
                            Case 1
'                                ssid = bc.StringFromBytes(b2)
                                ssid = bc.StringFromBytes(bc.SubString2(b2, 0, bc.IndexOf(b2, "&")))
                            Case 2
                                password = bc.StringFromBytes(b2)
                        End Select
                        i2 = i2 + 1
                    Next
                  
                End If
                i1 = i1 + 1
            Next
            Log("SSID: ", ssid)
            Log("Pass: ", password)
 
Upvote 0
Top