位元組流轉16進位制字串

快雪發表於2024-06-18
std::string bytes2string(const char* bytes, const int length)
{
    std::stringstream ss;
    ss << "\n";
    for (int i = 0; i < length; ++i) 
    {
        ss << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)(*(bytes + i)& 0xff) << " ";
        if ((i+1) % 16 == 0)
        {
            ss << "\n";
        }
    }
    ss << "\n";
    return ss.str();
}

輸出效果:

相關文章