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
 

LucaMs

Expert
Licensed User
Longtime User
I assume that the array is an Int (or long), you should use a function to convert hex to Int (I never use the hex code, but I am sure that you will find some functions on the site for this purpose.)

Something like:
B4X:
If InBuffer(0) = fnHexToInt(0xA5) AND InBuffer(1) = fnHexToInt(0x5a) Then
'do something, but we never end up here
end If
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
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

stevel05

Expert
Licensed User
Longtime User
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

MLDev

Active Member
Licensed User
Longtime User
If InBuffer() is an integer array this'll work:

B4X:
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

Arf

Well-Known Member
Licensed User
Longtime User
sorry last thing, just found the java data types.
Is there no unsigned 8 bit type?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Unfortunately not. you could use a sub to do the comparison, something like:
B4X:
if bytearray(0) = Signedbyte(0x5a) then
.
.
.
End If

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