0

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])); } } }

2nd Mar 2020, 8:10 PM
systography
4 Respostas
0
Brother, Thanks a lots for your valuable reply. Problem not solved. followed your instruction but result same as before. Test Result: Original Array: 01020A0F050C Compressed Array: 12 AF 5C Uncompressed Array: 0102FA0F050C NOTE: bit just changed. { FA, it should give 0A } Test Link: https://code.sololearn.com/c27nQ35yE5ZF
3rd Mar 2020, 6:08 AM
systography
0
no man, dont be sorry. you did great. just wow.... amazing. Thanks a lotz. really so much thankfull to you. I am director of systography Ltd and sipOsys Ltd. Well established registered Software Company in Dhaka, Bangladesh. i have a opportunity for you. contact with me. Email: siposys@gmail.com
3rd Mar 2020, 3:36 PM
systography
0
// 6 bytes of input works fine but if i increase bytes, then its giving wrong result again. // code is dynamic now // inputs will be bytes packets. input packet size is (32767 * 2) // i actually want to use it in a vpn app. compiler: https://code.sololearn.com/c27nQ35yE5ZF/#java public class Program { public static void main(String[] args) { byte[] input = { 1, 2, 0xA, 0xF, 5, 0xC }; uncompressed(compressed(input)); } public static byte[] compressed(byte[] bytes) { System.out.println("Original Array: "); for (int i = 0; i < bytes.length; i++) { System.out.print(String.format("%02X ", bytes[i])); } byte[] compressed = new byte[bytes.length / 2]; for (int i = 0; i < compressed.length; ++i) { // get always two bytes from the original array int idx = 2 * i; byte a1 = bytes[idx]; byte a2 = bytes[idx + 1]; // now shift the first byte 4 binary digits to the left a1 <<= compressed.length + 1; // or a1*=16 // add then add the bitwise OR of both bytes to our compressed array compressed[i] = (byte) (a1 | a2); } System.out.println("\n"); System.out.println("Compressed Array: "); for (int i = 0; i < compressed.length; ++i) { System.out.print(String.format("%02X ", compressed[i])); // System.out.println("\n"); } System.out.println("\n"); System.out.println("Size:" + compressed.length); return compressed; } private static byte[] uncompressed(byte[] compressed) { // now uncompress the compressed again byte[] uncompressed = new byte[compressed.length * 2]; for (int i = 0; i < uncompressed.length / 2; ++i) { int idx = 2 * i; // get the first bits uncompressed[idx] = (byte) (compressed[i] >> uncompressed.length / 2 + 1 & 0x0F); // get the last bits uncompressed[idx + 1] = (byte) (compressed[i] & 0x0F); } // print uncompressed System.out.print("Uncompressed Array: "); System.out.println("\n"); for (int i = 0; i < uncompressed.length; ++i) { System.out.print(Str
3rd Mar 2020, 6:54 PM
systography
0
thanks a lot. i solved this code. public class Program { public static void main(String[] args) { byte[] input = { 1, 2, 0xA, 0xF, 5, 0xC, 6, 0xD, 7, 0xE, 7, 0xE }; uncompressed(compressed(input)); } public static byte[] compressed(byte[] bytes) { System.out.println("Original Array: "); for (int i = 0; i < bytes.length; i++) { System.out.print(String.format("%02X ", bytes[i])); } System.out.println("\nSize:" + bytes.length); byte[] compressed = new byte[bytes.length / 2]; for (int i = 0; i < compressed.length; ++i) { // get always two bytes from the original array int idx = 2 * i; byte a1 = bytes[idx]; byte a2 = bytes[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("\n"); System.out.println("Compressed Array: "); for (int i = 0; i < compressed.length; ++i) { System.out.print(String.format("%02X ", compressed[i])); // System.out.println("\n"); } System.out.println("\nSize:" + compressed.length); System.out.println("\n"); return compressed; } private static byte[] uncompressed(byte[] compressed) { // now uncompress the compressed again byte[] uncompressed = new byte[compressed.length * 2]; for (int i = 0; i < uncompressed.length / 2; ++i) { int idx = 2 * i; // get the first bits uncompressed[idx] = (byte) (compressed[i] >> 4 & 0x0F); // get the last bits uncompressed[idx + 1] = (byte) (compressed[i] & 0x0F); } // print uncompressed System.out.print("Uncompressed Array: \n"); //System.out.println("\n"); for (int i = 0; i < uncompressed.length; ++i) {
4th Mar 2020, 3:29 PM
systography