Skip to content

Instantly share code, notes, and snippets.

@vurdalakov
Created October 6, 2016 08:16
Show Gist options
  • Save vurdalakov/00f689a3c3fa07e13b2a84a0a91af023 to your computer and use it in GitHub Desktop.
Save vurdalakov/00f689a3c3fa07e13b2a84a0a91af023 to your computer and use it in GitHub Desktop.
Convert binary array to hex string
#pragma once
#include <windows.h>
#include <string>
// Converts a binary array to a hex string.
inline std::string bin2hex(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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment