Decode the message from Data packets sent over the network
Last Updated :
01 Nov, 2022
Over the network, messages are sent as data packets. Each data packet is a series of bytes (8bits = 1 byte). The task is to decode the data packet (str) of length N. The format of the data packet is as follows:
- The packet can be broken into 2 parts.
- The first byte represents a unique value, say K.
- The remaining bytes in the data packet represent the message to be decoded.
- Each message byte is to be XORed to the first byte, which results in a set of alphanumeric ASCII characters to get the readable message.
If the length of the data packet is not valid return -1.
Examples:
Input: str = “101010101110001011001111110001101100011011000101”, N = 48
Output: Hello
Explanation: Here K = 10101010, and the other bytes are:
“11100010”, “11001111”, “11000110”, “11000110”, “11000101”.
The Xor value with K are “01001000”, “01100101”, “01101100”, “01101100”, “01101111”
which have ASCII values 72, 101, 108, 108, 111 and the characters are ‘H’, ‘e’, ‘l’, ‘l’ and ‘o’.
Input: str = “1010101011100010111000101111”, N = 28
Output: -1
Explanation: The length of the packet is not valid. One byte of the message is missing some bits.
Approach: the solution to the problem is based on implementation. Check for the validity of the packet length. If valid, then do as said in the question. Follow the steps mentioned below:
- Check if the input length is divisible by 8, if not, return -1.
- Now, for the first byte i.e first 8 bits, calculate the decimal value and store it in a variable.
- Now, for every coming byte, XOR it with the first byte stored and print the corresponding character which has the ASCII value.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string decode(string str, int N)
{
if (N % 8 != 0)
return "-1" ;
string res = "" ;
int j = 0, k = 0, first = 0, K = 0;
for ( int i = 0; i < N; i++) {
k++;
if (k == 8) {
k = 0;
string s
= str.substr(j, i + 1 -j);
bitset<32> b(s);
int n = ( int )(b.to_ulong());
j = i + 1;
if (first == 1) {
int z = n ^ K;
char ch = ( char )z;
res += ch;
}
else if (first == 0) {
first = 1;
K = n;
}
}
}
return res;
}
int main()
{
string str = "101010101110001011001111110001101100011011000101" ;
int N = 48;
string ans = decode(str, N);
cout<<ans;
}
|
Java
import java.util.*;
public class Main {
static String decode(String str, int N)
{
if (N % 8 != 0 )
return "-1" ;
String res = "" ;
int j = 0 , k = 0 , first = 0 , K = 0 ;
for ( int i = 0 ; i < N; i++) {
k++;
if (k == 8 ) {
k = 0 ;
String s
= str.substring(j, i + 1 );
int n
= Integer.parseInt(s, 2 );
j = i + 1 ;
if (first == 1 ) {
int z = n ^ K;
char ch = ( char )z;
res += ch;
}
else if (first == 0 ) {
first = 1 ;
K = n;
}
}
}
return res;
}
public static void main(String[] args)
{
String str = "101010101110001011001111110001101100011011000101" ;
int N = 48 ;
String ans = decode(str, N);
System.out.println(ans);
}
}
|
Python3
def decode(strr: str , N: int ) - > str :
if N % 8 ! = 0 :
return "-1"
res = ""
j, k, first, K = 0 , 0 , 0 , 0
for i in range (N):
k + = 1
if k = = 8 :
k = 0
s = strr[j : i + 1 ]
n = int (s, 2 )
j = i + 1
if first = = 1 :
z = n ^ K
ch = chr (z)
res + = ch
elif first = = 0 :
first = 1
K = n
return res
if __name__ = = "__main__" :
strr = "101010101110001011001111110001101100011011000101"
N = 48
ans = decode(strr, N)
print (ans)
|
C#
using System;
using System.Data;
public class GFG {
static String decode(String str, int N)
{
if (N % 8 != 0)
return "-1" ;
String res = "" ;
int j = 0, k = 0, first = 0, K = 0;
for ( int i = 0; i < N; i++) {
k++;
if (k == 8) {
k = 0;
String s
= str.Substring(j, i + 1-j);
int n
= Convert.ToInt32(s,2);
j = i + 1;
if (first == 1) {
int z = n ^ K;
char ch = ( char )z;
res += ch;
}
else if (first == 0) {
first = 1;
K = n;
}
}
}
return res;
}
public static void Main(String[] args)
{
String str = "101010101110001011001111110001101100011011000101" ;
int N = 48;
String ans = decode(str, N);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function decode(str, N)
{
if (N % 8 != 0)
return "-1" ;
let res = "" ;
let j = 0, k = 0, first = 0, K = 0;
for (let i = 0; i < N; i++) {
k++;
if (k == 8) {
k = 0;
let s = str.slice(j, i + 1);
let n = parseInt(s, 2);
j = i + 1;
if (first == 1) {
let z = n ^ K;
let ch = String.fromCharCode(z);
res += ch;
}
else if (first == 0) {
first = 1;
K = n;
}
}
}
return res;
}
let str = "101010101110001011001111110001101100011011000101" ;
let N = 48;
let ans = decode(str, N);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(logN)
Share your thoughts in the comments
Please Login to comment...