Android Question [Resolved] How would I read into multiple numeric vars from a btye array?

DataMiser

Member
Licensed User
I am receiving a UDP packet which contains mixed numeric values, some are Int, some are float, some are byte.
I did a search and located the byte convertor lib but that does not seem to allow for mixed data types from the byte array, or perhaps I am just overlooking something.
In total the byte array is a bit over 300 bytes, most of the data is 32bit floats but a few are byte and a few are int32 and int16.

So how would I go about getting the data from the incoming UDP packet and getting it into the proper vars?

Any help is appreciated.
 

DataMiser

Member
Licensed User
Ok So I tried this.
B4X:
Sub UDP_PacketArrived (Packet As UDPPacket)
   Dim msg As String
   Dim tData(12) As Byte
   Dim fData() As Float

   bc.ArrayCopy(Packet.Data,244,tData,0, 3)
   fData=bc.FloatsFromBytes(tData)
   Label4.Text=fData.Length
   Label5.Text=Packet.length
   'msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
   Label1.Text=fData(0)
   Label2.Text=fData(1)
   Label3.Text=fData(2)
  
   'EditText2.Text = msg
End Sub

Under process globals I have
B4X:
Dim bc As ByteConverter

I am seeing 3 for the array size as expected. 311 for the packet size as expected.
I am seeing numbers in fdata(0) but not the numbers I expect and 0s in the fdata(1) and fData(2)

I have tried changing the offset up 1 and down 1. The working code I have in VB.Net uses offset 244 but it seems that if I set the offset here to 243,244 or 245 I am never seeing the numbers I expect in the fdata(0) and getting 0s in the other two.

These are the three values I am trying to get at the moment from my vbcode
B4X:
 Public Speed As Single
 Public Power As Single
 Public Torque As Single
and the code that reads them from the array
B4X:
Speed = BitConverter.ToSingle(msg, ptr) : ptr += toSingle ' As Single
        Power = BitConverter.ToSingle(msg, ptr) : ptr += toSingle ' As Single
        Torque = BitConverter.ToSingle(msg, ptr) : ptr += toSingle ' As Single
ptr = 244 when it gets to the section of code above

Any idea where I am going wrong here?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Do you know if the packet is BigEndian or LittleEndian? You will need to configure byteconverter appropriately. Coming from a PC it is likely to be LittleEndian, the default for Android is BigEndian.
 
Upvote 0

DataMiser

Member
Licensed User
Do you know if the packet is BigEndian or LittleEndian? You will need to configure byteconverter appropriately. Coming from a PC it is likely to be LittleEndian, the default for Android is BigEndian.
I set little endian to True as the source uses a Windows system. Still was not getting the correct result.

Looks like I had the wrong idea about the 3rd argument on the array copy method. I had it set to 3 thinking that would copy 3 floats over. Apparently it was getting 3 bytes instead and that obviously wasn't working right. I changed the 3 to 12 and am now seeing the data I expect in those 3 values.
 
Upvote 0
Top