1
0C 1C 34 38 34 24 12 1C 24 10 1B 0E 0A 1D 25
 C   S   2   6   2        I    S        G   R   E   A   T   !

Translating the above message into individal 8-bit bytes, we get:

00001100 00011100 00110100 00111000 00110100 00100100 00010010 00011100 00100100 00010000 00011011 00001110 00001010 00011101 00100101

Since we are going to only use six bits per character, we can remove the two Most Significant Bits (MSBs) with the following result:

001100 011100 110100 111000 110100 100100 010010 011100 100100 010000 011011 001110 001010 011101 100101

Now comes the tricky part... We will pack the bits into 8-bit bytes. These bytes will be part of our Packed Array. To do this, we start at the Least Significant Bit (LSB) of the first character. In this case, it is the letter C with the bit pattern 001100. The LSB of this pattern is the last 0 (bolded) - 001100. This bit will become the lsb of the first element in the packed array. We continue, bit by bit, until we have put the entire character in the first element of the packed array. Note that there will be two bits that are not yet initialized: - - 0 0 1 1 0 0 <- PackedArray[0] (started)

Now we start on the next character, or 011100 (The letter 'S'). Starting at the LSB, we put that bit in the next available bit of PackedArray[0]: - 0 0 0 1 1 0 0, then the next LSB after that: 0 0 0 0 1 1 0 0, and we have finished the first element of PackedArray. However, we have not finished packing the letter 'S'! We still have four bits left to encode! So, those four bits will then become the least significant bits of the next element of PackedArray:

        • 0 1 1 1 <- PackedArray[1] (started)

Continuing with the third character (the character '2'), we take the four LSBs of that character, and add them to the remaining bit locations:

0 1 0 0 0 1 1 1 <- PackedArray[1] (completed)

I’m having trouble to understand how fill in the bits with the LSB to shrink the numbers of bits

Mike
  • 4,041
  • 6
  • 20
  • 37
  • This is not a bad question, but it has nothing to do with `C` explicitly. The `C` tag suggests you either _intend_ to use that language in some implementation, or that you tried something already. If so, post your attempt, then point out where the problem lies. ( [mcve]. ) – ryyker Apr 01 '20 at 12:17
  • https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c – Hans Passant Apr 01 '20 at 13:12
  • How does the `char` `C` translate to the value `0x0C` ? I am having difficulty in seeing how your translation from the string to the first series binary sequences you are using to represent them. It is certainly not ASCII encoding. (i.e. ASCII `CS262` translates to HEX `43 53 32 36 32`, not `0C 1C 34 38 34`) – ryyker Apr 01 '20 at 14:16

1 Answers1

0

Ok, so here is some ugly code to do what you want. There is a ton of improvements you can make. The best thing that I would read up for to better understand this problem is how Base 64 works. This is basically just teaching you some very important base 64 concepts.

As the comments say, your conversions do not make sense. I just treated your string as base ASCII since I didn't understand what you meant.

I verified half of my results manually and they can be seen here.

Verified results

Program results

Credit to sth (https://stackoverflow.com/users/56338/sth) for the reversal code found here In C/C++ what's the simplest way to reverse the order of bits in a byte?

#include <stdio.h>

unsigned char reverse( char b) {
    b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
    b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
    b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
    return b;
}

int main()
{
unsigned char input[16] =  "CS262 IS GREAT!";
unsigned char reverseInput[16] =  {0};
unsigned char output[16] = {0};

printf("%s is the input\n\n", input);

for(int i=0; i<15;i++)
{
    printf("%X ",input[i]);
}

for(int i=0; i<15;i++)
{
    reverseInput[i] = reverse(input[i]);
}

printf("\n");

for(int i=0; i<15;i++)
{
    printf("%X ",reverseInput[i]);
}
 printf("\n");
unsigned int place = {0};

for(int i=0;i<4;i++)
{

    place = (reverseInput[i*4] & 0x3F) << 26 | (reverseInput[i*4+1] & 0x3F) << 20  | (reverseInput[i*4+2] & 0x3F) << 14 | (reverseInput[i*4+3] & 0x3F) << 8;

    printf("%08X ",place);
    output[(i*3)] = place >> 24;
    output[(i*3)+1] = place >> 16;
    output[(i*3)+2] = place >> 8;
}

printf("\n\nPacked Array: ");
for(int i=0; i<15;i++)
{
    printf("%X ",output[i]);
}


return 0;

}

PatrickOfThings
  • 237
  • 1
  • 9
  • Please note, I know the many limitations of this piece of code. I didn't want to take the time to optimize and scale it. Just wanted to give a proof of concept. – PatrickOfThings Apr 02 '20 at 17:41