Android Question byte check via notation

Bert Oudshoorn

Member
Licensed User
The check on 0xFF fails in an if statement.
It looks as if it considered as integer, a check on -1 works.
The check on 0xFF works in a Case Select

Dim b As Byte = 0xFF
If b = 0xFF Then 'a test on -1 works
Msgbox("0xFF check OK","")
Else
Msgbox("???","")
End If

PS: The subject had to be "byte check via hex notation"
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The byte type holds numeric values between -128 to 127. In most cases you can ignore this fact. However for this to work properly you need to compare bytes to bytes:
B4X:
Dim b1 As Byte = 0xFF
Dim b2 As Byte = 0xFF
Log(b1 = b2)

Another option:
B4X:
Log(b1 = ToByte(0xff))

Sub ToByte(i As Int) As Byte
   Return i
End Sub
 
Upvote 0

Bert Oudshoorn

Member
Licensed User
The byte type holds numeric values between -128 to 127. In most cases you can ignore this fact. However for this to work properly you need to compare bytes to bytes:
B4X:
Dim b1 As Byte = 0xFF
Dim b2 As Byte = 0xFF
Log(b1 = b2)

Another option:
B4X:
Log(b1 = ToByte(0xff))

Sub ToByte(i As Int) As Byte
   Return i
End Sub

Erel, thank you, especially the 2nd option is nice. But, that it also works in a Select Case. There the Case values are probably "type casted" to the Select "type".
 
Upvote 0
Top