Java Program to convert an UNSIGNED byte to a JAVA type

Java 8Object Oriented ProgrammingProgramming

<p>Firstly, let us declare byte values.</p><pre class="result notranslate">byte val1 = 127; byte val2 = -128;</pre><p>To convert the above given unsigned byte, you can use the following. Here, we are first implementing it for the variable &ldquo;val1&rdquo;.</p><pre class="result notranslate">(int) val1 &amp; 0xFF</pre><p>Now for the second variable &ldquo;val2&rdquo;.</p><pre class="result notranslate">(int) val2 &amp; 0xFF</pre><p>Let us see the complete example to convert an UNSIGNED bye to a JAVA type.</p><h2>Example</h2><p><a class="demo" href="" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">import java.util.*; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; byte val1 = 127; &nbsp; &nbsp; &nbsp; byte val2 = -128; &nbsp; &nbsp; &nbsp; System.out.println(val1); &nbsp; &nbsp; &nbsp; System.out.println((int) val1 &amp; 0xFF); &nbsp; &nbsp; &nbsp; System.out.println(val2); &nbsp; &nbsp; &nbsp; System.out.println((int) val2 &amp; 0xFF); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">127 127 -128 128</pre>
Updated on 26-Jun-2020 10:18:09