c++ buf 转 hex,内存转十六进制文本

int BufToHex(char* pBuf, int iLen, string& strOut)
{
  strOut.clear();
  if (NULL == pBuf || iLen < 1)
  {
    return TRUE;
  }

  char szTmp[4] = { 0 };
  strOut.reserve(128 + iLen * 3);
  for (int i = 0; i < iLen; i++)
  {
    ZeroMemory(szTmp, sizeof(szTmp));
    sprintf(szTmp, "%02X ", static_cast<BYTE>(pBuf[i]));
    strOut.append(szTmp);
  }

  return TRUE;
}

原文地址:https://www.cnblogs.com/longma8586/p/13861062.html