Last active
October 20, 2019 11:20
-
-
Save dipietrantonio/692fb689c129dd6995c6bcbd45ae8b32 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdlib.h> | |
// | |
// bin2hex | |
// Returns the hexadecimal representation of a bytes sequence. | |
// Parameters | |
// - bin: array of bytes | |
// - size: number of bytes in bin. | |
// Returns | |
// A string containing the hex representation of bin. | |
// | |
char *bin2hex(u_int8_t *bin, int size){ | |
const char *hexdigits = "0123456789abcdef"; | |
char *str = (char*) malloc(sizeof(char) * (size * 2 + 1)); | |
if(!str) return NULL; | |
for (int i = 0; i < size; i++){ | |
uint8_t v = bin[i]; | |
uint8_t v2 = v & 0x0F; | |
uint8_t v1 = v >> 4; | |
str[2*i] = hexdigits[v1]; | |
str[2*i + 1] = hexdigits[v2]; | |
} | |
str[size*2] = '\0'; | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment