I’m trying to implement a function which returns a string of hex values. I print the hex values out using this function:
void print_hex(unsigned char *hash, const hashid type) {
int i;
for (i = 0; i < mhash_get_block_size(type); i++) {
printf("%.2x", hex[i]);
}
printf("\n");
}
This outputs some hex value, e.g. 71c092a79cf30c4c7e7baf46a4af3c78cedec9ae3867d1e2600ffc39d58beaf2
How can I modify this function so that it returns a string? i.e.
unsigned char *get_hash_str(unsigned char *hash, const hashid type) { /* ?? */ }
(The goal being to compare the 2 values later)
Obviously the caller is responsible for
freeing the returned string.Still, as @K-Ballo correctly said in his answer, you don’t need to convert to string form two hashes to compare them, all you need in that case is just a
memcmp.