<p>In Java, there is no such thing as an unsigned byte.</p>

<p>Working with some low level code, occasionally you need to work with bytes that have unsigned values greater than 128, which causes Java to interpret them as a negative number due to the MSB being used for sign.</p>

<p>What's a good way to work around this? (Saying don't use Java is not an option)</p>

[{'answer_id': 11092, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': -1, 'selected': False, 'text': '<p>I guess you could just use a short to store them.  Not very efficient, but really the only option besides some herculean effort that I have seen.</p>\n'}, {'answer_id': 11098, 'author': 'stimms', 'author_id': 361, 'author_profile': 'https://Stackoverflow.com/users/361', 'pm_score': 0, 'selected': False, 'text': '<p>Probably your best bet is to use an integer rather than a byte.  It has the room to allow for numbers greater than 128 without the overhead of having to create a special object to replace byte.  </p>\n\n<p>This is also suggested by people smarter than me (everybody)</p>\n\n<ul>\n<li><a href="http://www.darksleep.com/player/JavaAndUnsignedTypes.html" rel="nofollow noreferrer">http://www.darksleep.com/player/JavaAndUnsignedTypes.html</a></li>\n<li><a href="http://www.jguru.com/faq/view.jsp?EID=13647" rel="nofollow noreferrer">http://www.jguru.com/faq/view.jsp?EID=13647</a></li>\n</ul>\n'}, {'answer_id': 11101, 'author': 'pauldoo', 'author_id': 755, 'author_profile': 'https://Stackoverflow.com/users/755', 'pm_score': 5, 'selected': True, 'text': "<p>When reading any single value from the array copy it into something like a short or an int and manually convert the negative number into the positive value it should be.</p>\n\n<pre><code>byte[] foobar = ..;\nint value = foobar[10];\nif (value &lt; 0) value += 256 // Patch up the 'falsely' negative value\n</code></pre>\n\n<p>You can do a similar conversion when writing into the array.</p>\n"}, {'answer_id': 15657, 'author': 'martinatime', 'author_id': 1353, 'author_profile': 'https://Stackoverflow.com/users/1353', 'pm_score': 0, 'selected': False, 'text': '<p>The best way to do bit manipulation/unsigned bytes is through using <strong>int</strong>s. Even though they are signed they have plenty of spare bits (32 total) to treat as an unsigned byte. Also, all of the mathematical operators will convert smaller fixed precision numbers to <strong>int</strong>. Example:</p>\n\n<pre><code>short a = 1s;\nshort b = 2s;\nint c = a + b; // the result is up-converted\nshort small = (short)c; // must cast to get it back to short\n</code></pre>\n\n<p>Because of this it is best to just stick with integer and mask it to get the bits that you are interested in. Example:</p>\n\n<pre><code>int a = 32;\nint b = 128;\nint foo = (a + b) | 255;\n</code></pre>\n\n<p>Here is some more info on Java primitive types <a href="http://mindprod.com/jgloss/primitive.html" rel="nofollow noreferrer">http://mindprod.com/jgloss/primitive.html</a></p>\n\n<p>One last trivial note, there is one unsigned fixed precision number in Java. That is the <strong>char</strong> primitive.</p>\n'}, {'answer_id': 15874, 'author': 'izb', 'author_id': 974, 'author_profile': 'https://Stackoverflow.com/users/974', 'pm_score': 1, 'selected': False, 'text': '<p>Using ints is generally better than using shorts because java uses 32-bit values internally anyway (Even for bytes, unless in an array) so using ints will avoid unnecessary conversion to/from short values in the bytecode.</p>\n'}, {'answer_id': 19186, 'author': 'ejack', 'author_id': 1377, 'author_profile': 'https://Stackoverflow.com/users/1377', 'pm_score': 6, 'selected': False, 'text': "<p>It is actually possible to get rid of the if statement and the addition if you do it like this.</p>\n\n<pre><code>byte[] foobar = ..;\nint value = (foobar[10] &amp; 0xff);\n</code></pre>\n\n<p>This way Java doesn't interpret the byte as a negative number and flip the sign bit on the integer also.</p>\n"}, {'answer_id': 5047679, 'author': 'octo', 'author_id': 282400, 'author_profile': 'https://Stackoverflow.com/users/282400', 'pm_score': 0, 'selected': False, 'text': '<p>I know this is a very late response, but I came across this thread when trying to do the exact same thing.  The issue is simply trying to determine if a Java byte is >127.</p>\n\n<p>The simple solution is:</p>\n\n<pre><code>if((val &amp; (byte)0x80) != 0) { ... }\n</code></pre>\n\n<p>If the real issue is >128 instead, just adding another condition to that if-statement will do the trick.</p>\n'}]
['https://Stackoverflow.com/questions/11088', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1309/']