Sign in to follow this  

odd problems with % operator result when turning it into a unsigned char variable

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This may not be the right forums for this question but it kind fits in a few places. This comes from a current project of mine which is a random height map generator. I recently modifyed it to work with heights beyond 255 so it is useing doubles to do this. I made a mini map that showed a greyscale image of it so i tryed to get it to show in greyscale then other color types to make it slightly more useful. First off is the code. It is cut a bit to show the code dealing with my problems (I mean there is more there but its not relevent). What is not shown is that buffer[] is a double pointer to a array of data, data[] is a pointer to a array of unsigned char, RemanderOutput is a file output object from ofstream, and Width and Height are int's defined else where

	int x, z, Remander, temp;

	x = 0;
	z = 0;

	for(x = 0; x < (Width * Height); x++) {

		temp = buffer[x];

		Remander = temp%255;

		if(buffer[x] <= 255) {

			Data[z] = buffer[x];
			z++;
			Data[z] = buffer[x];
			z++;
			Data[z] = buffer[x];
			z++;
		}//end buffer[x] <= 255 if

		//red if's
		if(buffer[x] > 255) {
			if(buffer[x] <= 510) {
				if(Remander == 0) {
					Data[z] = 255;
					z++;
					Data[z] = 0;
					z++;
					Data[z] = 0;
					z++;
				}//end Remander if

				else {

					RemanderOutput << "Remander: " << Remander << std::endl;
					Data[z] = Remander;
					RemanderOutput << "Data: " << Data[z] << std::endl;
					z++;
					Data[z] = 0;
					z++;
					Data[z] = 0;
					z++;
				}//end else
			}//buffer[x] <= 510
		}//buffer[x] > 255


When i run my program and make the heightmap i output the data for the else to a text file. Remander seems to have the right value. However when i assign it to Data[z] it gives me a value of . I am not real use to using the % operator. only done it a few times and never ran into this befor and any information on why this is happening would be apreciated

Share this post


Link to post
Share on other sites
It doesn't look like it has anything to do with the % operator. Rather, it has everything to do with the fact that you're outputting a char, which outputs an ASCII* character rather than the textual representation of an 8-bit number. If you want that, cast it to int. For instance:

char c = 65;
cout << c << endl; // Outputs "A"
cout << (int)c << endl; // outputs "65"

BTW: Your problems with localizing this bug are directly caused by the fact that you're using output statements to do your debugging. If you learn how to use the debugger, these issues will be trivial to pinpoint and fix.



*Or EBCDIC, if you're kicking it old school.

Share this post


Link to post
Share on other sites

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this