Found 34489 Articles for Programming

Convert a byte to hexadecimal equivalent in Java

Naveen Singh
Updated on 26-Jun-2020 10:16:08

1K+ Views

To convert a byte to hexadecimal equivalent, use the toHexString() method in Java.Firstly, let us take a byte value.byte val1 = (byte)90;Before using the method, let us do some more manipulations. Mask the byte value now:int res = val1 & 0xFF;Let us now see the complete example and use the toHexString() method to convert a byte to hexadecimal equivalent.Example Live Demopublic class Demo {    public static void main(String[] args) {       byte val1 = (byte)90;       System.out.println("Byte = "+val1);       int res = val1 & 0xFF;       System.out.println("Hexadecimal = "+Integer.toHexString(res));    } }OutputByte = 90 Hexadecimal = 5a

Java Program to convert an UNSIGNED byte to a JAVA type

Naveen Singh
Updated on 26-Jun-2020 10:18:09

169 Views

Firstly, let us declare byte values.byte val1 = 127; byte val2 = -128;To convert the above given unsigned byte, you can use the following. Here, we are first implementing it for the variable “val1”.(int) val1 & 0xFFNow for the second variable “val2”.(int) val2 & 0xFFLet us see the complete example to convert an UNSIGNED bye to a JAVA type.Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       byte val1 = 127;       byte val2 = -128;       System.out.println(val1);       System.out.println((int) val1 & 0xFF);       System.out.println(val2);       System.out.println((int) val2 & 0xFF);    } }Output127 127 -128 128

Java Program to compare two Byte Arrays

Naveen Singh
Updated on 26-Jun-2020 10:21:03

230 Views

To compare two Byte Arrays, use the Arrays.equals() method. Here we have declared and initialized a total of 4 arrays.byte[] arr1 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr2 = new byte[] { 12, 13, 34, 87, 99, 33}; byte[] arr3 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr4 = new byte[] { 13, 16, 56, 78, 98, 99 };Now, using the Arrays.equals() method, we will be compare two arrays.Arrays.equals(arr1, arr2);In the same way, other arrays would be compared one by one.Let us see the complete example to compare two ... Read More

Java Program to convert byte to string

Naveen Singh
Updated on 26-Jun-2020 10:21:30

423 Views

In Java, there are different ways to convert byte to string.Using toString() method, you can easily convert byte to string as shown below −Example Live Demopublic class Demo {    public static void main(String[] args) {       byte res = 87;       // byte to string       System.out.println(Byte.toString(res));    } }Output87In the above example, we have taken a byte value.byte res = 87;With that, to convert to string, the method toString() is used as shown below −Byte.toString(res);Let us see another example to convert byte to string.Example Live Demopublic class Demo {    public static void main(String[] ... Read More

Display the minimum of three integer values in Java

Naveen Singh
Updated on 26-Jun-2020 10:07:43

281 Views

The following is an example displaying the minimum of three integer values.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 99;       int val2 = 87;       int val3 = 130;       System.out.println("Number 1 = "+val1);       System.out.println("Number 2 = "+val2);       System.out.println("Number 3 = "+val3);       if (val2 < val1) {          val1 = val2;       }       if (val3 < val1) {          val1 = val3;   ... Read More

Display the maximum of three integer values in Java

Naveen Singh
Updated on 26-Jun-2020 10:08:31

161 Views

The following is an example displaying the maximum of three integer values.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 10;       int val2 = 20;       int val3 = 30;       System.out.println("Number 1 = "+val1);       System.out.println("Number 2 = "+val2);       System.out.println("Number 3 = "+val3);       if (val2 > val1) {          val1 = val2;       }       if (val3 > val1) {          val1 = val3;   ... Read More

Display Minimum and Maximum values of datatype int in Java

Naveen Singh
Updated on 26-Jun-2020 10:09:07

150 Views

To get the minimum value of datatype int in Java, use the following −Integer.MIN_VALUETo get the maximum value of datatype int in Java, use the following −Integer.MAX_VALUELet us now implement this in our example.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 20;       int val2 = 3000;       System.out.println("Value1: "+val1);       System.out.println("Value2: "+val2);       System.out.println("Maximum value: "+Integer.MIN_VALUE);       System.out.println("Minimum value: "+Integer.MAX_VALUE);    } }OutputValue1: 20 Value2: 3000 Maximum value: -2147483648 Minimum value: 2147483647

Java Program to check for Integer overflow

Naveen Singh
Updated on 26-Jun-2020 10:09:42

5K+ Views

To check for Integer overflow, we need to check the Integer.MAX_VALUE, which is the maximum value of an integer in Java.Let us see an example wherein integers are added and if the sum is more than the Integer.MAX_VALUE, then an exception is thrown.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 9898989;       int val2 = 6789054;       System.out.println("Value1: "+val1);       System.out.println("Value2: "+val2);       long sum = (long)val1 + (long)val2;       if (sum > Integer.MAX_VALUE) {          throw ... Read More

Java Program to convert from decimal to binary

Naveen Singh
Updated on 18-Jun-2024 15:20:46

21K+ Views

To convert decimal to binary, Java has a method Integer.toBinaryString(). The method returns a string representation of the integer argument as an unsigned integer in base 2.Let us first declare and initialize an integer variable.int dec = 25;Convert it to binary.String bin = Integer.toBinaryString(dec);Now display the “bin” string, which consists of the Binary value. Here is the complete example.Example Live Demopublic class Demo {    public static void main( String args[] ) {       int dec = 25;       // converting to binary and representing it in a string       String bin = Integer.toBinaryString(dec);       System.out.println(bin);    } }Output11001

Java Program to perform an XOR on a set of Booleans

Naveen Singh
Updated on 26-Jun-2020 10:11:38

133 Views

To perform XOR on a set of Booleans, firstly let us consider the following Boolean array.boolean[] arr = { true, true, false };Let us now create a nested loop and within that perform XOR operation.for (boolean one : arr) {    for (boolean two: arr) {       // XOR       boolean res = one ^ two;    } }Here is the entire example to displayXOR on a set of Booleans.Example Live Demopublic class Demo {    public static void main(String[] args) {       // boolean array       boolean[] arr = { true, true, ... Read More

Advertisements