Android Question logical AND

Arf

Well-Known Member
Licensed User
Longtime User
Hi,
Silly question probably.. I am just trying to check the value of two array entries, when I run the code the following line always amounts to a false, even though I can see that InBuffer(0) is 91 (0xA5) and InBuffer(1) is 90 (0x5A). Any ideas why?

If InBuffer(0) = 0xa5 AND InBuffer(1) = 0x5a Then
'do something, but we never end up here
End If

Thanks
 
Sorry, I meant to type -91. It works if I change the code to this:
If InBuffer(0) = -91 AND InBuffer(1) = 90 Then

but I would like to know how to acheive it writing numbers in base Hex, for future reference as I use hex a a lot.
 
Upvote 0
I think the problem is that the bytes in the byte array are signed, and the hex literals are obviously not, in fact they are type int.

log(0x5A) returns 90 but Log(0xA5) returns 165.

Whereas

Dim B As Byte = 0xa5
Log(B)

Returns -91
 
Last edited:
Upvote 0
If InBuffer() is an integer array this'll work:

Code: Copy
If InBuffer(0) = 0xFFFFFFA5 AND InBuffer(1) = 0x5a Then

Because integers are 32 bits.

Edit: after doing some testing that'll work with any type of numerical variable.
 
Last edited:
Upvote 0
sorry last thing, just found the java data types.
Is there no unsigned 8 bit type?
 
Upvote 0
Unfortunately not. you could use a sub to do the comparison, something like:
Code: Copy
if bytearray(0) = Signedbyte(0x5a) then
.
.
.
End If

Sub SignedByte(val As Int) As Byte
    Return Bit.AND(val,255)
End Sub
 
Upvote 0
Back
Top