Android Question int to Byte for use with async streams

woodpecker

Member
Licensed User
Longtime User
Hi all,

I'm stuggling with converting an int to a byte to use with async streams, I tried various things from the ByteConverter library without success.

I'm writing an array of bytes to astreams like this:-

AStreams.Write(Array As Byte (0x02, 0x4B, keycode, 0x03, 0x0D))

But keycode is an int eg (17 or 0x11) and I guess I need to convert it to a byte, I've tried using Conv.IntstoBytes, also tried converting it to a hex string, I'm sure this is quite simple, anyone help please?

TIA
 

udg

Expert
Licensed User
Longtime User
Two reminders for you:
- a byte has a range -127 to +128
- an int counts for 4 bytes
So, for your application, if you need to send out only significant bytes than you have to convert "keycode" to its lowest-byte counterpart (i.e. masking out the three highest valued bytes) and insert that into the stream. Otherwise just take in account on the receiving part that "keycode" counts for 4 bytes and not a single one.

udg
ps: somewhere in my mind there's a fading recall about an automatic conversion between integer and byte, but I can't clearly focus it right now
 
Upvote 0

woodpecker

Member
Licensed User
Longtime User
I don't think that there is any keycode larger than 255. You can send it like this:
B4X:
AStream.Write(Array As Byte(Keycode))

Thanks Erel and eveyone that replied, that worked fine for what I needed, I can't believe I didn't try that, I spent ages messing about thinking I need to do some conversion on the int.
 
Upvote 0
Top