Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
void bin_to_strhex(unsigned char *bin, uint32_t size)
{
char hex_str[]= "0123456789ABCDEF";
unsigned int i;
char *result;
result = (char *)malloc(size * 2 + 1);
(result)[size * 2] = 0;
if (!size)
return;
for (i = 0; i < size; i++) {
(result)[i * 2 + 0] = hex_str[(bin[i] >> 4) & 0x0F];
(result)[i * 2 + 1] = hex_str[(bin[i] ) & 0x0F];
}
//printf("result : %s\n", result);
for (int i=0, cnt=0; i < size * 2 + 1; i++) {
printf("%c", *(result + i));
if (++cnt == 8) {
cnt = 0;
printf(" ");
}
}
printf("\n");
free(result);
}
// demo code
unsigned char* bindata = (unsigned char *)(sendTask->pData + sendTask->uSentSize);
uint32_t binsz = sendTask->uDataSize - sendTask->uSentSize;
bin_to_strhex(bindata, binsz);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment