The Following code does not work as expected:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdint>
int main()
{
std::stringstream ss;
ss << std::setfill('0') << std::hex << std::setw(2) << std::int8_t(-1);
std::cout << ss.str() << std::endl;
}
What I want is to convert a signed char to a fixed length hex representation. The output is always 0�.
Converting to unsigned int also print unexpected output:
ss << std::setfill('0') << std::hex << std::setw(2) << (unsigned int)std::int8_t(-1);
This will print ffffffff which is correct but I told the stream to set the width to 2. Is there any way to work around this?