Dim UDPSocket1 As UDPSocket
End Sub
Sub Service_Create
UDPSocket1.Initialize("UDP", 5000, 8192)
Dim server2 As ServerSocket
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Destroy
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
Dim msgudp As String
msgudp=""
msgudp = BytesToString(Packet.data,0, 4,"UTF8")
CallSubDelayed2(Main, "receiveudp", Packet.data)
End Sub
Assuming that it is indeed a fixed-length string padded with NULs (ie ASCII 0x00), then
B4X:
Dim msg2 As String
msg2 = BytesToString(Result, 0, Result.Length, "UTF8").Replace(Chr(0), "")
If that doesn't work, then perhaps the �s are some other character (0x7F aka DEL is another common non-printable padding), so check using:
B4X:
Dim msg2 As String
msg2 = BytesToString(Result, 0, Result.Length, "UTF8")
Log("Last character is Chr(" & Asc(msg2.CharAt(msg2.Length - 1)) & ")")
Log("Last byte is " & Result(Result.Length - 1))
I would expect the same number in both those Logs, but... NUL is sometimes handled differently to other characters, particularly eg as an end-of-string marker, so I wouldn't like to stake my life on that expectation ;-)
Absolute 100 percent?!? Nothing warms my heart more than a bit of unbridled optimism.
But if things do happen to go awry, and you find that msgupd.Length is unexpectedly shorter than Packet.Length... remember that bytes with the high bit set usually represent a multibyte UTF-8 character, eg 2 bytes = 1 character.
But if you're just sending plain 7-bit ASCII, or actual UTF-8... then perhaps 100% is correct.