Android Question BitConverter

TADS

New Member
i have been searching on the forums and on google but can not really find a answer for our dev that dose the android apps for us... he has to create my windows app in android...

i use BitConverter in my app to read a game server to show information... i will post some of my windows vb.net code and could someone please helps us out on trying to use this code with android?

B4X:
 Dim returnData As String = Encoding.ASCII.GetString(data, 0, recv)


            sname = returnData.ToCharArray(20, 20)
            musers = BitConverter.ToInt32(data, 16)
            users = BitConverter.ToInt32(data, 208)
            port = BitConverter.ToInt16(data, 52)
            Track = returnData.ToCharArray(69, 15)
            wear = returnData.ToCharArray(131, 2)
            real = returnData.ToCharArray(86, 10)
            raceLen = returnData.ToCharArray(111, 7)
            raceLenper = returnData.ToCharArray(106, 4)
            passwd = BitConverter.ToInt16(data, 55)
            flags = returnData.ToCharArray(98, 5)

musers for example is is max users that can enter a server... i use the returnData with BitConverter to convert to readable Data.

Is there something in B4A that can do the same thing?

Thanks
Kev
 

dualznz

Member
Licensed User
Longtime User
Hi
I am the developer of this application with tads

This below code is the code i am working from in VB.net and trying to convert it into B4A
B4X:
 Try
            Dim data As Byte() = New Byte(1023) {}
            'Dim input As String, stringData As String
            Dim recv As Integer
            Dim ipep As New IPEndPoint(IPAddress.Parse(srvip), usedport)

            Dim server As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)

            Dim sockopt As Integer = CInt(server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout))
            server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000)
            sockopt = CInt(server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout))

            Dim welcome As String = "tadstools"
            data = Encoding.ASCII.GetBytes(welcome)
            server.SendTo(data, data.Length, SocketFlags.None, ipep)

            Dim sender As New IPEndPoint(IPAddress.Any, 0)
            Dim tmpRemote As EndPoint = DirectCast(sender, EndPoint)
            data = New Byte(1023) {}

            recv = server.ReceiveFrom(data, tmpRemote)


            Dim returnData As String = Encoding.ASCII.GetString(data, 0, recv)


            sname = returnData.ToCharArray(20, 20)
            rstart = BitConverter.ToInt32(data, 196)
            Thin = BitConverter.ToInt32(data, 64)
            musers = BitConverter.ToInt32(data, 16)
            users = BitConverter.ToInt32(data, 208)
            port = BitConverter.ToInt16(data, 52)
            Track = returnData.ToCharArray(69, 15)
            wear = returnData.ToCharArray(131, 2)
            real = returnData.ToCharArray(86, 10)
            raceLen = returnData.ToCharArray(111, 7)
            raceLenper = returnData.ToCharArray(106, 4)
            passwd = BitConverter.ToInt16(data, 55)
            flags = returnData.ToCharArray(98, 5)
            If passwd = "1" Then
                passwd = "Required"
            Else
                passwd = "Not Required"
            End If
            If Thin = "0" Then
                Thin = "Open"
                rstart = "UNDEFINED"
            Else
                Thin = "racing"
            End If
            passlab.Text = passwd.Trim
            userslab.Text = users + "/" + musers
            serverlab.Text = sname.Trim
            Tracklab.Text = Track.Trim
            wearlab.Text = wear.Trim
            reallab.Text = real.Trim
            statuslab.Text = Thin.Trim
            modlab.Text = Mod_name.Trim
            ' torlab.Text = Type.Trim
            racelenlab.Text = raceLen + " (" + raceLenper + ")"
            Flagslab.Text = flags


            ' ListBox1.Items.Add("RACERS IN SERVER NOW!")
            If users > 0 Then
                ' ListBox1.Items.Add(" ")
                Dim i As Integer
                Dim subst As Integer = 188
                Dim offset As Integer = 24
                Dim num As Integer = subst
                Dim usrinsrv As String = users - 2


                For i = 0 To users.Length + usrinsrv
                    For Each playername In users
                        num = num + offset
                        ListBox1.Items.Add(returnData.Substring(num, 12).Trim & ControlChars.NewLine)

                    Next
                    '  MsgBox(userlist)
                Next
                bestlap = BitConverter.ToInt32(data, 204)
                ' bestlap = returnData.Substring(252, 4)
                ' bestlap = returnData.Substring(228, 4)
                ' best = byte4toint(best)
                ' Console.WriteLine(best)
                'Console.WriteLine("best lap time... " + bestlap & ControlChars.NewLine)
                ' Leaguelab.Text = bestlap.ToString


            Else
                usersinserver = "NONE!"
                ListBox1.Items.Add(usersinserver)
            End If

            server.Close()
        Catch
            ' MsgBox("server not open")

        End Try

As you can see on rstart Thin musers and so on we are using the bitconverter to pick out positions in the received bytes and then converting them into a readable int.

But we are unsure on how to use the byte converter library for b4a to produce the same results as the VB.net code.

I have also looked at the minimal documentation at http://www.b4x.com/android/help/byteconverter.html#byteconverter_littleendian

But does not really help when trying convert as there are no actual examples on that documentation.

Any help would be great

Cheers
 
Last edited:
Upvote 0

TADS

New Member
i ended up writing to sub/Functions to read them and it is now all working!

B4X:
Sub Byte4toint (buffer() As Byte, Offset As Int)
Dim Byte4 As Int
Byte4 = buffer(Offset)
Byte4 = Bit.AND(Byte4,0x7F)
Return Byte4
End Sub

Sub Byte2toint (buffer() As Byte, Offset As Int)
Dim Byte2 As Int
Byte2 = buffer(Offset)
Byte2 = Bit.AND(Byte2,0x7FF)
Return Byte2
End Sub
 
Upvote 0
Top