Join Regular Classroom : Visit ClassroomTech

IBM Question Solved | Packet Decoder | Codewindow.in

Question:

Over the networks, the messages are sent as data packets which  has a series of bytes(8 bits=1 byte). This problem also expects you to work on the data packets and decode the message. The format of the data packet is as follows:

The packet can be broken into 3 parts.

The first 8 bits(1byte) of the represents the length of the data in the packet. The length is computed from the 3rd byte  of the packet; the first 2 bytes are ignored.

Remaining bytes in the input packet represent the message to be decoded.

Each message byte is to be XORed to the first byte, which results in set of alphanumeric ASCII characters to get the readable message.

 

The goal of the program is to decode this input packet and print the actual message sent across.

Consider the example, Where  the input packet is as below:

10101010000001011110001011001111110001101100011011000101

 

For ease of understanding, let us insert a whitespace after each byte, and the packet appears as below:

10101010   00000101   11100010   11001111   11000110   11000110   11000101

 

Now the first byte is 10101010 and this will be used for XORing the message bytes. The second byte is 00000101 and this gives the length of data which is 5 for this input when converted to decimal. The remaining bytes are decoded as:

 

11100010 ^ 10101010 = 01001000 -> H

11001111 ^ 10101010 = 01100101 -> e

11000110 ^ 10101010 = 01101100 -> l

11000110 ^ 10101010 = 01101100 -> l

11000101 ^ 10101010 = 01101111 -> o

 

On the right side of above decoded string is given the ASCII equivalent character. Thus , the message in the packet decodes to Hello.

Also note that the number of characters is same as length specified in the meta information. Should this length not match, then the following the message should be printed on the console:

Invalid packet

 

Function Description

Complete the function decodepacket in the editor below. The function must state what must be returned or printed.

Decodepacket has the following parameter(s):

packetData: a series of binary integers which represent both meta information and the message in the packet.

 

Constraints

  • The input packet has a length of at least 2 bytes; even if there is no message in the packet. That is, The length of message is 0(zero)

  • The length of the message should match the length in the meta information.

Sample Case 0:

Sample input For Custom Testing

10101010000001011110001011001111110001101100011011000101

Sample Output

Hello

Explanation

  1. The first byte represent the key to XOR

  2. The second byte is the length of the message in the packet.

  3. The remaining bytes are extracted from the packet and each byte is XORed with the first byte and the message is extracted.

Input:

10101010000001011110001011001111110001101100011011000101

Output:

Hello

Solution:

JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class CodeWindow
{
    static int toDecimal(String str) {
        int res = 0;
        for(int i=0;i<str.length();i++) {
            int val = str.charAt(i)-48;
            res = res*2+val;
        }
        return res;
    }
    
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str1 = br.readLine();
		int key = Integer.parseInt(str1.substring(0, 8));
		String len = str1.substring(8, 16);
		int length = toDecimal(len);
		int index = 16;
		for(int i=0;i<length;i++) {
		    int code = Integer.parseInt(str1.substring(index, index+8));
		    index +=8;
		    int val = toDecimal(Integer.toString(key)) ^ toDecimal(Integer.toString(code));
		    System.out.print((char)val);
		}
	}
}

C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;

int toDecimal(string str) {
    int res = 0;
    for(int i=0;i<str.length();i++) {
        int val = (int)(str.at(i))-48;
        res = res*2+val;
    }
    return res;
}
    
int main()
{
	// your code goes here
	string str1;
	cin >> str1;
	int key = stoi(str1.substr(0, 8));
	string len = str1.substr(8, 8);
    int length = toDecimal(len);
	int index = 16;
	for(int i=0;i<length;i++) {
        int code = stoi(str1.substr(index, 8));
		index +=8;
		int val = toDecimal(to_string(key)) ^ toDecimal(to_string(code));
		cout << (char)val;
	}
}

Also Checkout

Recent Posts
Categories
Pages