Created
October 6, 2016 08:16
-
-
Save vurdalakov/00f689a3c3fa07e13b2a84a0a91af023 to your computer and use it in GitHub Desktop.
Convert binary array to hex string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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