int main () {
  char k = 0xd8;
  cout << hex << k << endl;
}
For some reason this prints out the character form of k and not the hex form of d8. Doing cout << hex << (int) k << endl; gives me ffffd8 which is not the one I want.
int main () {
  char k = 0xd8;
  cout << hex << k << endl;
}
For some reason this prints out the character form of k and not the hex form of d8. Doing cout << hex << (int) k << endl; gives me ffffd8 which is not the one I want.
 
    
     
    
    This is simply one of the many deficiencies in C++, and another reason why I stick with C when I can. You can read more about the topic in the references I provided below. To save yourself some hassle, consider just going with the printf() family of functions in production code, as the fix isn't guaranteed to be standards-compliant.
Code Listing
#include <iostream>
using namespace std;
int main(void)
{
    unsigned char k = 0xd8;
    cout << "k = 0x" << hex << +k << endl;
}
Sample Output
k = 0xd8
References
http://cpp.indi.frih.net/blog/2014/08/code-critique-stack-overflow-posters-cant-print-the-numeric-value-of-a-char/, Explicit C++, Accessed 2015-09-27http://cpp.indi.frih.net/blog/2014/09/tippet-printing-numeric-values-for-chars-and-uint8_t/, Explicit C++, Accessed 2015-09-27