I\'m noticing that cout << hex is giving me strange results, and I cannot find anywhere that answers why. What I am doing is simply assigning some values to
cout << hex
The other answers are correct about the reason. The simplest fix is:
cout << hex << +a << endl;
Demonstration: http://ideone.com/ZHHAHX
It works because operands undergo integer promotion.