another bitwise operator question

Started by
3 comments, last by LordFallout 17 years ago
Hello, i have a binary pattern written to a file, it is written backwards . . so 11000101 is written to the file, 1 then 0 then 1 then 0 etc. I want to read this binary back from the file into an unsigned char, however i need to read it back into a varibale backwards too, so i need to read it in 1 then 0 then 1 then 0 and store the result as an unsigned char, i think i might need to use bitwise operations but im not too sure any help would be lovely. Thank you [Edited by - LordFallout on March 23, 2007 2:30:24 PM]
Advertisement
This page proposes a simple way of reversing the bits in an integer, but it could be easily adapted to a char.
buffer[n] |= BinNumber << i;

i got what i wanted like this, i do recieve some irratic behaviour though :S
So, did you get what you wanted, or not? If not, please post the input, the expected result, and the actual result.
Ok, well i have two text files and an image, i hide one text file in an image by changing random pixels, i change the LSB of each pixel too match a single bit of text byte, for a file with 1 byte of text, i.e one character of type unsigned char i store 8 bits within the picture i tried to hide this text

This is a Hidden text message, it is not hidden anymore.

and recieved this text

This is a Hidden text message, it ) J Rp6>hÊF

it is the right size just somwhere the text gets muddled up. I input the binary like so

	RandX = new int[FileSize * 8];	RandY = new int[FileSize * 8];	RandomGenerator->GetPairs( RandX, RandY, FileSize * 8, Dest->Picture->Bitmap->Width, Dest->Picture->Bitmap->Height );	for (int y=0; y < Dest->Picture->Bitmap->Height; y++)	{		for (int x=0; x < Dest->Picture->Bitmap->Width; x++)		{			if( x == RandX[PixelIndex] && y == RandY[PixelIndex] )			{				int shiftedNumber = buffer[n];				BinNumber = (shiftedNumber >> i) & 1;				Pixel.Colours = Dest->Canvas->Pixels[x][y];				ColorToRGB(Pixel.Colours);				CurrentColour = GetRValue(Pixel.Channel.Red);				CurrentColour = CurrentColour & ~1 | BinNumber;				Pixel.Channel.Red = CurrentColour;				Dest->Canvas->Pixels[x][y] = Pixel.Colours;				Pixel.Colours = Dest->Canvas->Pixels[x][y];				ColorToRGB(Pixel.Colours);				CurrentColour = GetRValue(Pixel.Channel.Red);				PixelIndex++;				x = 0;				y = 0;				i++;				if( i > 7 )				{					n++;					i = 0;				}			}		}	}


and output like so

	RandX = new int[FileSize * 8];	RandY = new int[FileSize * 8];	RandomGenerator->GetPairs( RandX, RandY, FileSize * 8, Dest->Picture->Bitmap->Width, Dest->Picture->Bitmap->Height );	for (int y=0; y < Dest->Picture->Bitmap->Height; y++)	{		for (int x=0; x < Dest->Picture->Bitmap->Width; x++)		{			if( x == RandX[PixelIndex] && y == RandY[PixelIndex] )			{				Pixel.Colours = Dest->Canvas->Pixels[x][y];				ColorToRGB(Pixel.Colours);				CurrentColour = GetRValue(Pixel.Channel.Red);				BinNumber = (CurrentColour >> 0) & 1;				buffer[n] |= BinNumber << i;				PixelIndex++;				x = 0;				y = 0;				i++;				if( i > 7 )				{					buffer[n + 1] = 0;					n++;					i = 0;				}			}		}	}


I cant really understand how only some of the text gets messed up, i can provide more of an explanation if needed. It confuses me a little typing it haha

edit: the filesize is set depending on the size of the input file.

This topic is closed to new replies.

Advertisement