Android Question Data tyes and casting,

Arf

Well-Known Member
Licensed User
Longtime User
I'm struggling a bit with data types when I mix types and do maths.. I'm trying to create a circular buffer to handle my received bluetooth data.
B4X:
Dim OutBufRef As Int = 0 '(this is in process globals)

B4X:
Sub PeepByte (indx As Char)As Byte
    Dim val As Byte
    Dim newRef As Char
    newRef = OutBufRef + indx     'This is the offending line

    If newRef >= 128 Then
        newRef = newRef - 128       
    End If
    val = InBuffer(newRef)
    Return val   
End Sub

Looking at the code above, when I step through in debug and OutBufRef = 6 and I call PeepByte(4), the value that ends up in newRef is 1 (just after I pass the "this is the offending line" point.

So it's a char (16bit unsigned value) that gets a value of 1, when signed 32bit with the value of 6 gets added to a signed 8 bit with the value of 4. I know its a horrible mix of types, but the outcome is still surprising to me and I'd like to definitively understand what I can or can't do with regards mixing types and basic maths.

Off to scour the manual.. thanks.
 

Arf

Well-Known Member
Licensed User
Longtime User
Thanks Erel, int worked. Its a function in a group of functions that allow cyclic buffering, this function lets me peep at historical buffer content without removing bytes from it and incrementing the outPointer, handy for making decisions as whether to whether to extract a packet (from bluetooth) and deal with it, or leave it until the whole packet has arrived (i'm not using prefix mode). Working beautifully now.
 
Upvote 0
Top