Here is a code sample that I am using to print the contents of a uint8_t
vector in hex format.
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <vector>
std::ostream& operator<<(std::ostream& o, const std::vector<uint8_t>& packet) {
std::ios_base::fmtflags oldFlags = o.flags();
std::streamsize oldPrec = o.precision();
char oldFill = o.fill();
o << std::showbase // show the 0x prefix
<< std::internal // fill between the prefix and the number
<< std::setfill('0'); // fill with 0s
for (std::size_t i = 0; i < packet.size(); ++i) {
o << std::setw(4) << std::hex << (int) packet[i] << " ";
}
o.flags(oldFlags);
o.precision(oldPrec);
o.fill(oldFill);
return o;
}
int main() {
std::vector<uint8_t> packet{0x81, 0x55, 0x00};
std::cout << packet << "\n";
}
This prints the following:
0x81 0x55 0000
I want that last 0000
to show up as 0x00
. How can I achieve this?
PS: I looked at couple of questions regarding printing 0x00
but answers on those questions prints 0000
for me as well.