Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include struct Entry { unsigned char character; int length; unsigned short encoding; } ; unsigned short concatenateUnsignedShort ( unsigned short first, unsigned short

#include
#include
#include
struct Entry {
unsigned char character;
int length;
unsigned short encoding;
};
unsigned short concatenateUnsignedShort(unsigned short first, unsigned short second){
return (first <<(sizeof(unsigned short)*8))| second;
}
// Function to print binary representation with leading zeros and spaces
void printBinary(unsigned short value, int length){
for (int j =11; j >=11- length +1; j--){
printf("%d",(value >> j) & 1);
}
}
unsigned short binaryConversion(unsigned short value, int length){
unsigned short bin;
for (int j =11; j >=11- length +1; j--){
bin =((value >> j) & 1);
}
return bin;
}
int main(){
// Decoding table
struct Entry table[256];
// Read the number of entries in the decoding table
unsigned char T_hex;
scanf("%x", &T_hex);
// Interpret T_hex as an unsigned integer
unsigned int T = T_hex;
// If T is 0, interpret it as 256
if (T ==0){
T =256;
}
// Read and populate the decoding table
for (int i =0; i < T; i++){
unsigned char c1, c2, c3;
// Read each byte individually
scanf("%hhx %hhx %hhx", &c1, &c2, &c3);
table[i].character = c1;
// Correctly extract the length using bitwise AND and shifting
table[i].length =(c2>>4) & 0x0F;
table[i].encoding =((c2 & 0x0F)<<8)| c3;
}
// Read four bytes encoding the length of the compressed bitstream
unsigned int compressedLength;
unsigned char b1, b2, b3, b4;
scanf("%hhx %hhx %hhx %hhx", &b1, &b2, &b3, &b4);
compressedLength =(b1<<24)|(b2<<16)|(b3<<8)| b4;
if (compressedLength !=0){
// Output the decoding table in the specified format
for (int i =0; i < T; i++){
printf("0x%2.2x : %d ", table[i].character, table[i].length);
// Use the printBinary function to print the binary representation
printBinary(table[i].encoding, table[i].length);
printf("
");
}
}
int numDecoded =0;
int done =0;
char binaryArr[60];
while(!done){
unsigned char hexValue;
scanf("%hhx", &hexValue);
char binaryRep[9];
for (int i =7; i >=0; i--){
binaryRep[7- i]=((hexValue >> i) & 1)+'0';
}
binaryRep[8]='\0';
int bitstreamLength = sizeof(binaryRep)-1; // Exclude the null terminator
// Variable to represent encoding D
unsigned short currentEncoding =0;
int currentLength =0;
// Process the bitstream
for (int i =0; i < bitstreamLength; i++){
// Read the next bit from the bitstream
unsigned char nextBit = binaryRep[i]-'0';
// Add the next bit to the current encoding
currentEncoding =(currentEncoding <<1)| nextBit;
currentLength++;
// printf("%u %u
", currentEncoding, currentLength);
int foundIndex =-1;
unsigned short accumalated =0;
char *arr[60];
int index =0;
for (int j =0; j < T && currentLength <=12; j++){
unsigned short bin = binaryConversion(table[j].encoding, table[j].length);
accumalated =(accumalated <<1)| bin;
printf("%u %d
", accumalated, table[j].length);
if (table[j].length == currentLength && accumalated == currentEncoding){
foundIndex = j;
break;
}
}
if (foundIndex !=-1){
printf("%c", table[foundIndex].character);
// Reset current encoding and length
currentEncoding =0;
currentLength =0;
// Increment the number of decoded characters
numDecoded++;
//printf("%d
", numDecoded);
}
if(numDecoded == compressedLength){
done =1;
break;
}
}
}
printf("
");
return 0;
} here i am trying to concatenate the values of bin. for eg if bin for j =0 is 1, then accumulate will be 1. then when bin for j =1 is 0, then accumulate becomes 10 if for j =3 bin is 0, then accumulate becomes 100. but it is not concatenating.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Evaluate the importance of diversity in the workforce.

Answered: 1 week ago

Question

Identify the legal standards of the recruitment process.

Answered: 1 week ago