Dim os As OutputStream
Dim lastPos As Int 'keep track of last position written to in the outputstream
Dim Conv As ByteConverter 'need ByteConverter library
Sub AddIntToPacket (NewInt As Int)
Dim IntArray(1) As Int
IntArray(0) = NewInt
Dim ba As Byte()
ba=Conv.IntsToBytes(IntArray)
os.WriteBytes(SwapEndian(ba), lastPos, 4) '4 is length of an int in bytes
lastPos=lastPos+4
End Sub
'delphi is little endian, Java/B4a is big endian
Sub SwapEndian(be As Byte()) As Byte()
Dim LEArray(be.Length) As Byte()
For i = 0 to be.Length-1
LEArray(i) = be(be.Length-1-i)
Next
Return LEArray()
End Sub
'length = desired padded length of string, in this case 100
Sub AddStringToPacket(NewString As String, length As Int)
If NewString.Length <= length Then
Do Until NewString.Length = length
NewString = NewString & Chr(0)
Loop
Else
ToastMessageShow("String too long", True)
End If
os.WriteBytes(Conv.StringToBytes(NewString, "US-ASCII"), lastPos, length)
lastPos=lastPos+length
End Sub