ADVERTISEMENT
ADVERTISEMENT

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.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.