Can anybody solve this in Java ? input output need to be same.
//this code is giving wrong result. //there is a code in C and that is working good. below link. // https://code.sololearn.com/chtXRPxR0yTk/#cpp //java code public class Program { public static void main(String[] args) { byte[] input = { 1, 2, 0xA, 0xF, 5, 0xC }; System.out.println("Original Array: "); for (int i = 0; i < input.length; i++) { System.out.println(String.format("%02X",input[i])); } byte[] compressed = new byte[3]; for (int i = 0; i < 3; ++i) { // get always two bytes from the original array int idx = 2 * i; byte a1 = input[idx]; byte a2 = input[idx + 1]; // now shift the first byte 4 binary digits to the left a1 <<= 4; //or a1*=16 // add then add the bitwise OR of both bytes to our compressed array compressed[i] = (byte) (a1 | a2); } System.out.println("Compressed Array: "); for (int i = 0; i < 3; ++i) { System.out.println(String.format("%02X",compressed[i])); } //now uncompress the compressed again byte[] uncompressed= new byte[6]; for(int i = 0; i< 3; ++i) { int idx = 2*i; //get the first bits uncompressed[idx] = (byte) (compressed[i]>>4); //get the last bits uncompressed[idx+1] = (byte) (compressed[i] & 0x0F); } //print uncompressed System.out.println("Uncompressed Array: "); for(int i = 0; i < 6; ++i) { System.out.println(String.format("%02X",uncompressed[i])); } } }