I have developed some free standing functions to provide hex dumps for buffer (std::string) contents.You can see it working in action here.The purpose is for debugging diagnostics to print buffer contents to a (logging) stream, in a format similar as wireshark does.Any proposals for improvements are welcome.Here's the code:#include <iostream>#include <iomanip>#include <cctype>std::ostream& render_printable_chars(std::ostream& os, const char* buffer, size_t bufsize) {    os <<  | ;    for (size_t i = 0; i < bufsize; ++i)    {        if (std::isprint(buffer[i]))        {            os << buffer[i];        }        else        {            os << .;        }    }    return os;}std::ostream& hex_dump(std::ostream& os, const uint8_t* buffer, size_t bufsize, bool showPrintableChars = true){    auto oldFormat = os.flags();    auto oldFillChar = os.fill();    os << std::hex;    os.fill('0');    bool printBlank = false;    size_t i = 0;    for (; i < bufsize; ++i)    {        if (i % 8 == 0)        {            if (i != 0 && showPrintableChars)            {                render_printable_chars(os, reinterpret_cast<const char*>(&buffer[i] - 8), 8);            }            os << std::endl;            printBlank = false;        }        if (printBlank)        {            os << ' ';        }        os << std::setw(2) << std::right << unsigned(buffer[i]);        if (!printBlank)        {            printBlank = true;        }    }    if (i % 8 != 0 && showPrintableChars)    {        for (size_t j = 0; j < 8 - (i % 8); ++j)        {            os <<    ;        }        render_printable_chars(os, reinterpret_cast<const char*>(&buffer[i] - (i % 8)), (i % 8));    }    os << std::endl;    os.fill(oldFillChar);    os.flags(oldFormat);    return os;}std::ostream& hex_dump(std::ostream& os, const std::string& buffer, bool showPrintableChars = true){    return hex_dump(os,reinterpret_cast<const uint8_t*>(buffer.data()), buffer.length(),showPrintableChars);}Here's the test case:int main(){    const char test[] = abcdef123456\0zyxwvu987654;    std::string s(test,sizeof(test));    hex_dump(std::cout, s);    return 0;}The output looks like expected:61 62 63 64 65 66 31 32 | abcdef1233 34 35 36 00 7a 79 78 | 3456.zyx77 76 75 39 38 37 36 35 | wvu9876534 00                   | 4.
Printing hex dumps for diagnostics
c++