For some testing purpose I want to display int8_t variables, cast them to uint8_t and display them again, in hexadecimal.
#include <iostream>
#include <sstream>
int main()
{
std::int8_t t = -1;
std::cout << std::hex << +t << std::endl;
std::cout << std::hex << +static_cast<std::uint8_t>(t) << std::endl;
return 0;
}
Here is my output for this program:
$ ./a.exe
ffffffff
ff
cout seems to format an int8_t as 4 bytes. Why is that? As it is a one-byte value, one would expect to see ff, like an uint8_t, which is the case in this example.