Android Question How to convert a byte to a single "byte" 0..255?

positrom2

Active Member
Licensed User
Longtime User
Bytes here are numbers ranging form -127...128 (or so). I need to send a number like 199 as a single "byte" to a another device that understands a byte as a number ranging from 0...255).
I found here that x=Bit.AND(199, 0xFF) does the conversion. But how to dim x in order to obtain that single "byte"?
 

Beja

Expert
Licensed User
Longtime User
Hi Erel,

IN Electronics, it is very difficult to think in decimal values.. we either think in binary or in hex.. in fact hex is only a representation of a binary value in a short form. So if I want to close relays 1 to 4, open relay 5 and 6 and close relays 7 and 8, I can immediately define the binary value that's 0x30 (00110000) but it is very difficult to use decimal (or integer) if it's possible at all, it needs a lot of imagination. In VB we use something like MSComm1.Output = &h30.. I can immediately see the binary equivalent, and know which relays will be open and which will be closed.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
In B4a/B4j you can use the literal 0x30.
 
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks Steve..
You mean can just write: outputstream.writebytes(0x30) ?

That's easy
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Outputstream.writebytes requires a byte array, so you would have to do:

B4X:
outputstream.writebytes(Array As byte(0x30) ,0,1)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The literal 0x30 is just that, it doesn't have to be Dimmed, it passes it's value to whatever variable you assign it to so:

B4X:
Dim a As Int = 0x30
Log(a)

Dim s As String = 0x30
Log(s)

Will both output 48
 
Upvote 0
Top