B4J Question Getting binary data with Astreams

SimonElck

Member
Licensed User
I probably get this all wrong,
but I send byte arrays from an Arduino
and I end up receiving Character strings in B4J (???!)
The source is based upon the Chat example
B4X:
Sub AStream_NewData (Buffer() As Byte)
    
    For Each n As Byte In Buffer
    txtLog.Text=txtLog.Text & Asc(n) & " "
    Next
    txtLog.Text=txtLog.Text  &CRLF
    txtLog.SetSelection(txtLog.Text.Length, txtLog.Text.Length)

End Sub
The relevant Arduino sketch part is:
B4X:
byte buf1[6]={1,2,3,13,10};
   Serial.write( buf1,5);

I expect the result to be:
1 2 3 13 10
But I get:
49 50 51 49 49

In the arduino serial monitor everything seems like I expected.
Does Astream do any translations, and how do I prevent that, or is there something else going on?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code takes the raw bytes, converts them to characters and then converts them to the ascii code (actually Unicode code points) of those characters.

Why???

The best way to show bytes is with ByteConverter.HexFromBytes (ByteConverter is a B4A / B4J library).

If you just want to show the numbers then remove the Asc keyword.
 
Upvote 0
Top