# Java program to extract bytes from an integer (Hex) value

Given an integer value in the hexadecimal format, we have to extract bytes from an integer (Hex) value.
Submitted by Nidhi, on March 02, 2022

Problem Solution:

In this program, we will extract bytes from an integer (Hexadecimal) value and print extracted bytes.

Program/Source Code:

The source code to extract bytes from an integer (Hex) value, is given below. The given program is compiled and executed successfully.

```// Java program to extract bytes from
// an integer (Hex) value

public class Main {
public static void main(String[] args) {
int value = 0x11223344; //4 Bytes value

int a, b, c, d; //to store byte by byte value

a = (value & 0xFF); //extract first byte
b = ((value >> 8) & 0xFF); //extract second byte
c = ((value >> 16) & 0xFF); //extract third byte
d = ((value >> 24) & 0xFF); //extract fourth byte

System.out.printf("First Byte  = %02X\n", a);
System.out.printf("Second Byte = %02X\n", b);
System.out.printf("Third Byte  = %02X\n", c);
System.out.printf("fourth Byte = %02X\n", d);
}
}
```

Output:

```First Byte  = 44
Second Byte = 33
Third Byte  = 22
fourth Byte = 11
```

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we extracted the bytes from an integer number and printed the result.

Preparation

What's New

Top Interview Coding Problems/Challenges!