字节流转16进制字符串

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();
}

输出效果:

 

posted @ 2024-06-18 16:23  快雪  阅读(14)  评论(0编辑  收藏  举报