int main(){
char c=0371;
cout<<hex<<(int) c;
return 0;
}
I converted c into binary system(011 111 001) then hexadecimal (f9). Then why does it give the result fffffff9 not f9?
int main(){
char c=0371;
cout<<hex<<(int) c;
return 0;
}
I converted c into binary system(011 111 001) then hexadecimal (f9). Then why does it give the result fffffff9 not f9?
If the char
type on your system is signed, the value 0xf9 is a negative number (specifically, it’s -7). As a result, when you convert it to an integer, it gives the integer the numeric value -7, which has hexadecimal representation 0xFFFFFFF9 (if you’re using signed 32-bit integer representations).
If you explicitly make your character value an unsigned char
, then C++ will cast it as though it has the positive value 249, which has equivalent integer value 249 with hexadecimal representation 0x000000F9.