I need to print the exact hex value of a char variable using C++.
Here is my code:
#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <iomanip>
int main()
{
char val = 0xfe;
std::stringstream ss1;
std::stringstream ss2;
ss1 << "#645" << std::setfill('0') << std::setw(2) << std::hex << val;
ss2 << "#645" << std::setfill('0') << std::setw(2) << std::hex << (int) val;
std::cout << ss1.str() << std::endl;
std::cout << ss2.str() << std::endl;
}
And my output:
#6450�
#645fffffffe
And what I expected:
#645FE