Question

I am having an issue with reading this character from a text file that was written in ios::binary mode. I used the notation unsigned char (181) to write it into the file, but when I try to get the decimal value for that with this line

cout << text[i] << " " << (unsigned int)text[i] << " " << (int)text[i] << endl;

I get out this..

╡ 4294967221 -75

This seems to be happening with all of my values over 127. And that symbol is the correct symbol that corresponds to the number I used, but for some reason when I try to cast it as unsigned int or int I get a wrong number. Is there something special I need to do with the values in the ascii table over 127?

Était-ce utile?

La solution

Convert to unsigned char first, before converting to int (or unsigned int). Apparently, on your system, plain char is signed, which means that it takes values in the range [-128, 127] (typically, assuming an 8 bit char).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top