B4R Question byte array to int

jimseng

Active Member
Licensed User
Longtime User
hello, once again confused by byte arrays etc.

So if I send this via a serial:20,30,40,50
and I split it into a byte array
B4X:
For Each x() As Byte In bc.Split(d,",")
     
log((x)
    Next
I get the expected result. How would I get the values into an int (or an int array)? Nothing I have tried seems to make any sense.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sending strings instead of binary values is a mistake.

You need to first convert the array of bytes to a string and then it can be parsed:
B4X:
For Each x() As Byte In bc.Split("20,30,40,50", ",")
   Dim i As Int = bc.StringFromBytes(x)
   Log(i)
Next

Better solution:
'B4R
B4X:
Dim ints() As Int = bc.IntsFromBytes(buffer)

'B4J
B4X:
bc.LittleEndian = True
astream.Write(bc.ShortsToBytes(Array As Short(20, 30, 40, 50)))

Note that B4R Int is equivalent to Short on the other platforms (2 bytes).
 
Last edited:
Upvote 0
Top