CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2010

    Questions about COSC1030 class code sample

    Okay, so this project is done and over, so I'm not in a particular rush. I'm just trying to go through this code to figure out why some things are done in it. The code analyzes a BMP file and decodes a hidden message within it. I'll post the entire code and ask questions that have me stumped in between.

    // BMPDecoder.cpp
    // Retrieve coded message from BMP graphics file.  The "hidden" message is contained in the least significant
    // bits of the bitmap RGB data.  If a data item is odd, that's a one-bit, if even, that's a zero-bit. Combine
    // the bits from 8 contiguous data items together (big-endian order) to form a character.
    // End of message is a '$'.
    #include <iostream>
    using std::cerr;
    using std::cin;
    using std::cout;
    using std::endl;
    #include <fstream>
    using std::fstream;
    #include <ios>
    using std::ios;
    using std::ios_base;
    #include <string>
    using std::string;
    #include <limits>
    using std::numeric_limits;
    void OpenInputFile( fstream& In )
    	string FileName;
    	cout << "Enter input file name:  ";
    	cin >> FileName;
    	In.open( FileName.c_str(), ios::binary | ios::in);
    	if ( In.fail() )
    		throw( FILE_OPEN_FAILED );
    void ConfirmBMP( fstream& In )
    	char tmp1, tmp2;
    	if ( tmp1!='B' || tmp2!='M' || In.fail() )
    		throw( FILE_NOT_BMP );
    	unsigned int startBitmap;
    	In.read(reinterpret_cast<char* >(&startBitmap),4);
    	if ( In.fail() )
    		throw( FILE_NOT_BMP );
    ^-Here I have a question. I do not understand the purpose of the bolded lines. Why start with an unsigned int, only to reinterpret_cast it to a char pointer? Why read 4 bytes? What does seeking to this value accomplish?
    char Decode(char* theBytes)
    	int tmp=0;
    	int thebit;
    	for(int i=0; i<8; i++) {
    		thebit = static_cast<unsigned char>(theBytes[i])%2;
    		tmp = (tmp*2)+(thebit);
    	return static_cast<char>(tmp);
    ^-Why static_cast theBytes[i] to an unsigned char?
    void ReadDecodeDisplay( fstream& In )
    	char ByteList[8];
    	char TheChar;
    	bool done=false;
    	In.exceptions( ios_base::failbit );
    	while(!done) {
    		try {
    			In.read(ByteList, 8);
    			TheChar=Decode( ByteList );
    			if( TheChar=='$' )
    				cout << TheChar;
    		catch(ios_base::failure) {
    			if( In.eof() )
    int main()
    	fstream InputFile;
    	try {
    		OpenInputFile( InputFile );
    		ConfirmBMP( InputFile );
    		ReadDecodeDisplay( InputFile );
    	catch(BMPErrorCode ErrorCode) {
    		switch(ErrorCode) {
    			case FILE_OPEN_FAILED:
    				cerr << "Error attempting to open input file" << endl;
    			case FILE_NOT_BMP:
    				cerr << "Error in format of input file, not a BMP" << endl;
    				cerr << "Unknown error" << endl;
    	return 0;

  2. #2
    Join Date
    Oct 2006

    Re: Questions about COSC1030 class code sample

    1. The value to be read is a 32 bit integer stored at byte offset 10. To read the integer you would like to do as In.read( &startBitmap,4 ); but unfortunately read can only read bytes. The cast is used to overcome this by telling the compiler that you would like to treat the integer as just any consecutive 4 bytes. Fortunately the bytes are stored in the correct order so there's no need for a byte-swap.

    2. theBytes is an array of char's. Most compilers default char to be signed char and as far as I know the modulo-operator is not defined for signed integers. A better way (in my opinion) would have been to declare theBytes unsigned char's instead.

    Edit: Tried to find what's said about &#37; and signed values in the standard but failed. However see this link http://en.wikipedia.org/wiki/Modulo_operation
    Last edited by S_M_A; December 14th, 2010 at 04:02 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here

    Get your free MS compiler here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.