B4J Question [SOLVED] b4J how to read Uint32(4byte) and Float (4byte) from UTP packed_Arrived

stefanogoria

Member
Licensed User
Longtime User
Hello to all, I am looking for info about reading e convert data received from Packe_Arrived on UDP, any suggestions ?
Data are of two types Uint32(4 bytes) and Float(4 bytes)
Thanks in advance !!
 
Last edited:

stefanogoria

Member
Licensed User
Longtime User
Where does the data come from?

Post an example, what is the output of Log(bc.HexFromBytes(Packet.Data)) and what should be the numeric value?
Hi Erel thank you for the replay (you are always the best)!!
About Unit32 I have solved in this way:



CovertUint32:
Dim ff As ByteConverter
Dim xx As String=ff.HexFromBytes(Packet.Data)
dim val1 as Int = ConvertiHexToUint32(xx,0,4)
dim val2 as Int = ConvertiHexToUint32(xx,4,4)


Sub ConvertiHexToUint32(stringa As String, pos As Int, nn As Int) As Int
    Dim val As Int
    Dim cc As Int
    For a=0 To (nn*2)-1
        Try
            Dim tmp As String = stringa.CharAt(pos+a)
            val=tmp
        Catch
            val=0
            If tmp="A" Then val=10
            If tmp="B" Then val=11
            If tmp="C" Then val=12
            If tmp="D" Then val=13
            If tmp="E" Then val=14
            If tmp="F" Then val=15
        End Try
        cc=cc+val*Power(16,nn-a-1)
    Next
    Return cc
End Sub

Results are correct !!

The real problem is about float and double data.
I have found this https://en.wikipedia.org/wiki/IEEE_754-1985 reading about data protocol specification.
Data comes from UDP multicast packet and i receive well but I can't read correctly what's indicated as Float and Double.

I tryed many solution (randomaccess with ReadFloat for example) and many other convertions without succes.

Here an example of data

00000065000000840000018beb89130d000000010000000000000001000000000000000100000001000000000000000100000000000000000000000000000000 404608857afea3df4023b1d9ef3bd43400000000435e970a000000003fe76c8b3f6bc6a8000000003c0f497d0000000000000000000000000000018beb8912b000000000

I convet Packet.data in Exadecimal dump then at byte 168,8 I expect data from 0 to 360 (is heading data) at byte 128,16 GPS latitude and at byte 144,16 GPS longitude of north italy location

Can you help me?
Thanks
Stefano
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
Hi Erel thank you for the replay (you are always the best)!!
About Unit32 I have solved in this way:



CovertUint32:
Dim ff As ByteConverter
Dim xx As String=ff.HexFromBytes(Packet.Data)
dim val1 as Int = ConvertiHexToUint32(xx,0,4)
dim val2 as Int = ConvertiHexToUint32(xx,4,4)


Sub ConvertiHexToUint32(stringa As String, pos As Int, nn As Int) As Int
    Dim val As Int
    Dim cc As Int
    For a=0 To (nn*2)-1
        Try
            Dim tmp As String = stringa.CharAt(pos+a)
            val=tmp
        Catch
            val=0
            If tmp="A" Then val=10
            If tmp="B" Then val=11
            If tmp="C" Then val=12
            If tmp="D" Then val=13
            If tmp="E" Then val=14
            If tmp="F" Then val=15
        End Try
        cc=cc+val*Power(16,nn-a-1)
    Next
    Return cc
End Sub

Results are correct !!

The real problem is about float and double data.
I have found this https://en.wikipedia.org/wiki/IEEE_754-1985 reading about data protocol specification.
Data comes from UDP multicast packet and i receive well but I can't read correctly what's indicated as Float and Double.

I tryed many solution (randomaccess with ReadFloat for example) and many other convertions without succes.

Here an example of data

00000065000000840000018beb89130d000000010000000000000001000000000000000100000001000000000000000100000000000000000000000000000000 404608857afea3df4023b1d9ef3bd43400000000435e970a000000003fe76c8b3f6bc6a8000000003c0f497d0000000000000000000000000000018beb8912b000000000

I convet Packet.data in Exadecimal dump then at byte 168,8 I expect data from 0 to 360 (is heading data) at byte 128,16 GPS latitude and at byte 144,16 GPS longitude of north italy location

Can you help me?
Thanks
Stefano
Why not directly use ByteConverter to do that?

For Uint8: IntsFromBytes(Uint32-data)
For Float: FloatsFromBytes(Float-data) 'B4J float standard just is IEEE_754.
 
Upvote 0

emexes

Expert
Licensed User
Here an example of data

00000065000000840000018beb89130d000000010000000000000001000000000000000100000001000000000000000100000000000000000000000000000000 404608857afea3df4023b1d9ef3bd43400000000435e970a000000003fe76c8b3f6bc6a8000000003c0f497d0000000000000000000000000000018beb8912b000000000

I convert Packet.data in Exadecimal dump then
at byte 168,8 I expect data from 0 to 360 (is heading data)
at byte 128,16 GPS latitude and
at byte 144,16 GPS longitude of north italy location

Assuming that your offsets 168, 128, 144 are into the hex string ie twice the offsets into your raw packet bytes, then give this a burl:

Add these two helper functions:
Sub Extract4Bytes(Bytes() As Byte, Start As Int) As Byte()
    
    Return Array As Byte(Bytes(Start), Bytes(Start + 1), Bytes(Start + 2), Bytes(Start + 3))
    
End Sub


Sub Extract8Bytes(Bytes() As Byte, Start As Int) As Byte()
    
    Return Array As Byte(                                                       _
        Bytes(Start    ), Bytes(Start + 1), Bytes(Start + 2), Bytes(Start + 3), _
        Bytes(Start + 4), Bytes(Start + 5), Bytes(Start + 6), Bytes(Start + 7)  _
    )
    
End Sub
and then test with this code:
Dim bc As ByteConverter

Dim ExampleHex As String = "00000065000000840000018beb89130d000000010000000000000001000000000000000100000001000000000000000100000000000000000000000000000000404608857afea3df4023b1d9ef3bd43400000000435e970a000000003fe76c8b3f6bc6a8000000003c0f497d0000000000000000000000000000018beb8912b000000000"
    
Dim ExampleBytes() As Byte = bc.HexToBytes(ExampleHex)

For S = 0 To ExampleBytes.Length - 1 Step 4
    Dim FourBytes() As Byte = Extract4Bytes(ExampleBytes, S)
    Dim FourByteFloat As Float = bc.FloatsFromBytes(FourBytes)(0)
    
    If S = 84 Then
        Dim Before As String = "Heading"
        Dim After As String = Chr(176)
    Else
        Dim Before As String = "hex at " & (S * 2)
        Dim After As String = ""
    End If
    
    Log(Before & " = " & FourByteFloat & After)
Next

Dim Latitude  As Double = bc.DoublesFromBytes( Extract8Bytes(ExampleBytes, 64) )(0)   
Dim Longitude As Double = bc.DoublesFromBytes( Extract8Bytes(ExampleBytes, 72) )(0)

Log("hex at 128,16 as double = " & Latitude)
Log("hex at 144,16 as double = " & Longitude)

and you should end up here, heading south-west(ish 222.59°):

1700628517771.png
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Assuming that your offsets 168, 128, 144 are into the hex string ie twice the offsets into your raw packet bytes

I just realised that, because data is offset at integral multiples of their size (Float = 4 bytes, Double = 8 bytes), then is perhaps simpler as:

B4X:
Dim bc As ByteConverter

Dim ExampleHex As String = "00000065000000840000018beb89130d000000010000000000000001000000000000000100000001000000000000000100000000000000000000000000000000404608857afea3df4023b1d9ef3bd43400000000435e970a000000003fe76c8b3f6bc6a8000000003c0f497d0000000000000000000000000000018beb8912b000000000"
   
Dim ExamplePacket() As Byte = bc.HexToBytes(ExampleHex)

Log("Heading = "   & bc.FloatsFromBytes(ExamplePacket)(21) & Chr(176))    '21 * 4 bytes * 2 hex digits = 168
Log("Latitude = "  & bc.DoublesFromBytes(ExamplePacket)(8) & Chr(176))    '8 * 8 bytes * 2 hex digits = 128
Log("Longitude = " & bc.DoublesFromBytes(ExamplePacket)(9) & Chr(176))    '9 * 8 bytes * 2 hex digits = 144

Log output:
Waiting for debugger to connect...
Program started.
Heading = 222.58999633789062°
Latitude = 44.0665735°
Longitude = 9.847365833333335°
Program terminated (StartMessageLoop was not called).
 
Last edited:
Upvote 0

stefanogoria

Member
Licensed User
Longtime User
Hi guys, thanks to all, you are fantastik !!!
I have used this solution without considering the offset logic how indicated by "emexes".
I take data directly from udp packet without hextoBytes conversion !!
I read intsFromBytes, FloatsFromBytes and DoublesFromBytes perfectly.
All is working fine.
Thanks to all again
 
Upvote 0
Top