Signed Bytes and USB??

Essie5

Member
Licensed User
Longtime User
First off (and not the most appropriate place to mention this), but thank you for the AMAZING tool! This is totally the best money I have ever spent on any piece of programming software, period!

Sorry, had to get that off my chest. On to the question:

I was really shocked to see the lack of an unsigned Byte in B4A, but of course that's really Java's fault. That leads me to a bit of a quandary though, because what does that do to USB transfers????

Just looking at BulkTransfer for example:
BulkTransfer (Endpoint As android.hardware.usb.UsbEndpoint, Buffer() As Byte, Length As Int, Timeout As Int) As Int

Can we really only transfer signed bytes to the device??? Do I have this correct or am I missing something? Don't all USB devices expect unsigned bytes?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
As you wrote Basic4android uses the same type system as Java.

While it takes some time to get used to it, there is really no problem with sending unsigned bytes.
For example if the external device expects a byte value of 235 then the following code will work correctly:
B4X:
Dim b As Byte
b = 235
Log(b) 'will print -21
Send(b)

If you want to interpret the value of an "unsigned" byte then you can use this code:
B4X:
Sub ConvertSignedByteToUnsigned(b As Byte) As Int
    Return Bit.And(b, 0xFF)
End Sub
 
Upvote 0

Essie5

Member
Licensed User
Longtime User
Thank you Erel!

I actually tested exactly that last night (except I made a function that will accept an array of signed bytes and turn it into an array of "unsigned" integers) to see how the USB device would respond and all seems to work just fine. Sorry for the question then...I guess it was shock more than anything.

Thanks again for the cool application!
 
Upvote 0
Top