Q:

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

belongs to collection: Java Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to convert hexadecimal byte to decima... >>
<< Java program to find subtraction of two numbers us...