1

So i have an array of bits, basically 0's and 1's in a character array.

Now what I want to do is store these bits in an integer I have in another array (int array), but I'm not sure how to do this.

Here is my code to get the bits:

char * convertStringToBits(char * string) {
    int i;
    int stringLength = strlen(string);
    int mask = 0x80; /* 10000000 */
    char *charArray;
    charArray = malloc(8 * stringLength + 1);
    if(charArray == NULL) {
        printf("An error occured!\n");
        return NULL; //error - cant use charArray
    }

    for(i = 0; i < stringLength; i++) {
        mask = 0x80;
        char c = string[i];
        int x = 0;
        while(mask > 0) {
            char n = (c & mask) > 0;
            printf("%d", n);
            charArray[x++] = n;
            mask >>= 1; /* move the bit down */
        }
        printf("\n");
    }

    return charArray;
}

This gets a series of bits in an array {1, 0, 1, 1, 0, 0, 1} for example. I want to store this in the integers that I have in another array. I've heard about integers having unused space or something.

For Reference: The integer values are red values from the rgb colour scheme.

EDIT: To use this I would store this string in the integer values, later to be decoded the same way to retrieve the message (steganography).

8
  • 1
    What is the question ? Can you give a use case, which input, expected output and output you obtain ? Commented Jan 25, 2016 at 10:48
  • Do you want to convert all the bits in the bit array into a single integer? Commented Jan 25, 2016 at 10:49
  • @Garf365 I wrote what I will use it for in the edit Commented Jan 25, 2016 at 10:49
  • And can you give examples of what the string values passed into the above function are? Your conversion code looks strange - it's unusual to bit mask string characters. Bit masking is usually for integer values. Commented Jan 25, 2016 at 10:53
  • @madcrazydrumma you can iterate over that string table directly treated elements as ints Commented Jan 25, 2016 at 10:54

2 Answers 2

5

So you want to do LSB substitution for the integers, the simplest form of steganography.

It isn't that integers have unused space, it's just that changing the LSB changes the value of an integer by 1, at most. So if you're looking at pixels, changing their value by 1 won't be noticeable by the human eye. In that respect, the LSB holds redundant information.

You've played with bitwise operations. You basically want to clear the last bit of an integer and substitute it with the value of one of your bits. Assuming your integers range between 0 and 255, you can do the following.

pixel = (pixel & 0xfe) | my_bit;

Edit: Based on the code snippet from the comments, you can achieve this like so.

int x;
for (x = 0; x < messageLength; x++) {
    rgbPixels[x][0] = (rgbPixels[x][0] & 0xfe) | bitArray[x];
}

Decoding is much simpler, in that all you need to do is read the value of the LSB of each pixel. The question here is how will you know how many pixels to read? You have 3 options:

  1. The decoder knows the message length in advance.

  2. The message length is similarly hidden in some known location so that the decoder can extract it. For example, 16 bits representing in binary the message length, which is hidden in the first 16 pixels before bitArray.

  3. You use an end-of-message marker, where you keep extracting bits until you hit a signature sequence that signals you to stop. For example, eight 0s in a row. You must make sure that how long the sequence and whatever it will be, it mustn't be encountered prematurely in your bit array.

So say somehow you have allocated the size for the message length. You can simply get extract your bit array (after allocation) like so.

int x;
for (x = 0; x < messageLength; x++) {
    bitArray[x] = rgbPixels[x][0] & 0x01;
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I've meant! So I can iterate through my array of integers and just replace the last bit using this code?
Even then I think it makes the process more readable and abstract having a separate method to create the bit train?
How could I fix this issue?
@madcrazydrumma I've updated my answer to briefly discuss decoding as well. Note that in the decoding example, you may not necessarily start extracting your bit array at x=0. This depends on whether and where you have hidden the message length.
0

This converts a string to the equivalent int.

char string[] = "101010101";

int result = 0;

for (int i=0; i<strlen(string); i++)
    result = (result<<1) | string[i]=='1';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.