B4R Question How to convert data between B4R client & B4J server

rwblinn

Well-Known Member
Licensed User
Longtime User
Seeking for a hint how to convert data between B4R client & B4J server
Example:
B4R Client
B4X:
..
Dim celsiustemp As Double = 100.0 * volts - 50  
If ethClient.Connected Then
  'IS THIS CORRECT? Probably not as need to convert Double to Array As Byte = BUT HOW?
  Dim s As Byte = celsiustemp
  astream.Write(Array As Byte(s))
...

B4J Server
B4X:
Sub Astream_NewData (Buffer() As Byte)
  Dim newdata As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  'THIS IS NOT WORKING = newdata is empty BUT the buffer has data
  Log($"New Data received: ${newdata}"$)
  aStream.Write("Thank you for this useful information.".GetBytes("UTF8"))
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4R
B4X:
'byte converter from rRandomAccessFile library
astream.Write(bc.DoublesToBytes(Array As Double(celsuistemp))

B4J
B4X:
If Buffer.Length Mod 4 <> 0 Then
Log("Invalid data")
Else
Dim f() As Float = bc.FloatsFromBytes(Buffer)) 'B4X float = B4R double
Log(f(0))
End If

Another option which might look better but it is not:
B4R
B4X:
Dim s As String = celsuistemp 'you can use NumberFormat for more options
astream.Write(s.GetBytes)

B4J
B4X:
Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
 
Upvote 0
Top