I have a system that needs a 7 bit character string converted to an 8 bit string of binary ones and zeroes, in this fasion:
Encoding sample
My main task is to find a way of generating a boolean array of each character and its corresponding 7 bits in order to do manipulations on them. ideally this would have the text string passed as a parameter to the program. I have searched a number of places for this sort of setup but no-one has any great ideas, can you help?

Recommended Answers

All 3 Replies

From what I understand, an 'unsigned char' datatype will give you straight-up binary properties.. 8 bits, 1 byte, 0 to 255.

My main task is to find a way of generating a boolean array of each character

I think a pseudo-code for the task at hand could be this:

1. create an unsigned char c-string of desired characters

unsigned char cstring[5] = {'H', 'E', 'L', 'L', 'O', '\0'};

2. create a bool array that will equal (n*8) the size of your unsigned c-string

//5 characters, 8 'bits' per character (no need to account for null terminating character)
bool binary[5][8];

3. perform bit shifting and extraction on individual c-string elements in order to populate 8 elements of the bool array

//haven't done bit shifting/extraction in awhile, hope this is right
for(int i=0; i<5; i++)
{
     for(int j=0; j<8; j++)
     {
          //extract right-most bit
          cstring[i] & 1 = binary[i][j];
          //shift all bits right one place
          cstring[i] >> 1;
     }
}

4. you now have the bool array populated with the equivalent binary representation of ascii characters, feel free to make any additional bitwise operations you need.

I guess this should be:

unsigned char cstring[6] = {'H', 'E', 'L', 'L', 'O', '\0'};

Here is working code that compiles, runs, and tests good:

#include<iostream>
using namespace std;

int main()
{
    unsigned char cstring[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    bool binary[5][8];

    for(int i=0; i<5; i++)
    {
        cout << endl << cstring[i] << '\t' << (int)cstring[i] << '\t';

        for(int j=0; j<8; j++)
        {
            binary[i][j] = cstring[i] & 1;
            cstring[i] >>= 1;
            cout << binary[i][j];
        }
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.