How to print string as sequence of Hex bytes in Arduino

The following code will print a std::string to Serial as sequence of Hex characters. It can easily be adapted to work with other types of strings.

const char* cstr = str.c_str();
for (size_t i = 0; i < str.size(); i++)
{
    Serial.printf("%02X ", cstr[i]);
}

This code is based on our previous post How to print byte as two hex digits in Arduino