19 Aug How to convert an UNSIGNED byte to Java type
An UNSIGNED byte can be converted to a Java type. Let us first learn about the signed and unsigned byte values:
1 2 3 4 |
UNSIGNED byte: 0 to 255 SIGNED byte: -128 to 127 |
Following is an example to convert an UNSIGNED byte to Java type:
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { byte a = -1; System.out.print("UNSIGNED converted to Java type: "); System.out.println((int) a & 0xFF); } } |
Output
1 2 3 |
UNSIGNED converted to Java type: 255 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { byte a = -5; System.out.print("UNSIGNED converted to Java type: "); System.out.println((int) a & 0xFF); } } |
Output
1 2 3 |
UNSIGNED converted to Java type: 251 |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { byte a = -126; System.out.print("UNSIGNED converted to Java type: "); System.out.println((int) a & 0xFF); } } |
Output
1 2 3 |
UNSIGNED converted to Java type: 130 |
No Comments