This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Java MSB negative value

If I understand it right, in java you cannot define a value as unsigned int. So the MSB of a Byte will decide, if the value is positive or negative.

But I have to use all 8 Bits of the Byte for the value of the variable.

Does anyone have experience with this, and how is it possible still to send 8 Bit variables?

With the MCP everything is working fine. But it should work the same trough a mobile device.

Is it defined by the BLE protocol, that I must send bytes?

Could I also setup a characteristic to send and recieve floats or double?

What other opportunities do I have?

I tried it to mask with '& 0xFF'

links[1] = (byte) (i & (0xFF));

links[2] = (byte) ((i >> 8) & (0xFF));

links[3] = (byte) ((i >> 16) & (0xFF));

As example I take i = 201600

What I get:

links[1] = -128 (should be 0x80) <---Result is false

links[2] = 19 (should be 0x13) <---Result is right

links[3] = 3 (should be 0x03) <---Result is right

Did I wrong mask, or why its still negative and wrong value?

Parents
  • The BLE protocols deal only with raw bytes - or "octets" (ie, groups of 8 bits) - they neither know nor care how your application will interpret them (eg, as "signed" or "unsigned" or whatever).

    As you say, this is a Java issue; nothing to do with BLE (nor Android). And, as you note, others have managed to do it - so, hopefully, someone familiar with Java will be along soon to explain it...

Reply
  • The BLE protocols deal only with raw bytes - or "octets" (ie, groups of 8 bits) - they neither know nor care how your application will interpret them (eg, as "signed" or "unsigned" or whatever).

    As you say, this is a Java issue; nothing to do with BLE (nor Android). And, as you note, others have managed to do it - so, hopefully, someone familiar with Java will be along soon to explain it...

Children
No Data
Related