<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Java MSB negative value</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/10612/java-msb-negative-value</link><description>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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 03 Dec 2015 07:55:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/10612/java-msb-negative-value" /><item><title>RE: Java MSB negative value</title><link>https://devzone.nordicsemi.com/thread/39542?ContentTypeID=1</link><pubDate>Thu, 03 Dec 2015 07:55:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aed4330c-a4b8-4162-84b6-eaaf664daaf0</guid><dc:creator>Inspectron</dc:creator><description>&lt;p&gt;I think, I will have to split this 16 bytes in 4 Characteristics. Because if I would add the 16th byte to an int, I would need to shift the value &amp;gt;&amp;gt; 131072 times.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Java MSB negative value</title><link>https://devzone.nordicsemi.com/thread/39540?ContentTypeID=1</link><pubDate>Wed, 02 Dec 2015 19:32:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:98505b60-7cba-4bf7-a17f-e4fc3fc6d242</guid><dc:creator>Eric Stutzenberger</dc:creator><description>&lt;p&gt;You should be able to simply declare links with type of int instead of a type byte.  It is impossible in Java to represent an 8 bit value &amp;gt; 0x7F as an unsigned value unless you convert it to something with more than 8 bits.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Java MSB negative value</title><link>https://devzone.nordicsemi.com/thread/39539?ContentTypeID=1</link><pubDate>Wed, 02 Dec 2015 19:28:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d50627d6-bf2f-4226-8868-5649fbfc6e81</guid><dc:creator>Inspectron</dc:creator><description>&lt;p&gt;You are right it is a byte array with the size of 16 bytes. I will try to add 16 bytes in an int, but I think this will be to much.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Java MSB negative value</title><link>https://devzone.nordicsemi.com/thread/39541?ContentTypeID=1</link><pubDate>Wed, 02 Dec 2015 19:22:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:42ffd671-144a-4b38-ac4f-def38b357d56</guid><dc:creator>Eric Stutzenberger</dc:creator><description>&lt;p&gt;If links is a byte array, then it will still be interpreted as a negative value.  Try making links an int.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Java MSB negative value</title><link>https://devzone.nordicsemi.com/thread/39544?ContentTypeID=1</link><pubDate>Wed, 02 Dec 2015 17:24:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1d616024-4b1f-4c30-9299-843ae805fec7</guid><dc:creator>Eric Stutzenberger</dc:creator><description>&lt;p&gt;I&amp;#39;m not sure I fully understand your question so I&amp;#39;ll point out a few things I&amp;#39;ve learned in dealing with BLE characteristic data for Android:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Java, as you said, does not have the concept of unsigned anything which, for us embedded guys, is extremely annoying.  However, from a BLE perspective, you simply have to consider the fact that a negative value interpreted by Java requires 8 bits.  It is simply the MSb that makes it negative or positive.  As long as all of the bits are there, then the BLE APIs will transfer the correct value.  Nothing in the Android BLE API strips off this &amp;#39;negative marker bit&amp;#39;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you need to convert BLE characteristic values of say, 4 bytes, to some other value, then you&amp;#39;ll need to do some casting to make sure the value ends up being correct.  Here is a code snippet for how I handle this:&lt;/p&gt;
&lt;p&gt;int mTotalBytesSent = ((value[1] &amp;amp; 0xFF) + ((value[2] &amp;amp; 0xFF) &amp;lt;&amp;lt; 8) + ((value[3] &amp;amp; 0xFF) &amp;lt;&amp;lt; 16) + ((value[4] &amp;amp; 0xFF) &amp;lt;&amp;lt; 24));&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The important part in this is the &amp;#39;&amp;amp; 0xFF&amp;#39; mask of the value.  I initially did this operation without the mask and received some very strange values.&lt;/p&gt;
&lt;ol start="3"&gt;
&lt;li&gt;If you don&amp;#39;t want to do it the manual way, Android provides some conversion functions to convert a characteristic&amp;#39;s bytes into certain types of values.  There are functions such as getIntValue, getFloatValue, and getStringValue.  All of these take an offset parameter and the number methods also required a type where you can specify types such as FORMAT_UINT8, FORMAT_UINT32, etc.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int val = someChar.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hope this helps,&lt;/p&gt;
&lt;p&gt;Eric&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Java MSB negative value</title><link>https://devzone.nordicsemi.com/thread/39543?ContentTypeID=1</link><pubDate>Wed, 02 Dec 2015 16:29:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:57ba0e60-14bc-4d07-a19b-2bddbb7e3852</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;The BLE protocols deal only with raw bytes - or &amp;quot;octets&amp;quot; (ie, groups of 8 bits) - they neither know nor care how your application will interpret them (eg, as &amp;quot;signed&amp;quot; or &amp;quot;unsigned&amp;quot; or whatever).&lt;/p&gt;
&lt;p&gt;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...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>