Sunday, November 23, 2014

Encode binary array as a hex string

std::string hexEncode(unsigned char* buffer, int bufferSize)
{
    std::string hex;
    hex.reserve(bufferSize * 2 + 1);

    const char chars[] = "0123456789ABCDEF";

    for (int i = 0; i < bufferSize; i++)
    {
        unsigned char b = buffer[i];
        hex += chars[(b >> 4) & 0x0F];
        hex += chars[b & 0x0F];
    }

    return hex;
}

No comments:

Post a Comment