Skip to content

Instantly share code, notes, and snippets.

@tony0x59
Created October 2, 2016 13:14
Show Gist options
  • Save tony0x59/6871baa40c1cae39b0fd2b470e5ff418 to your computer and use it in GitHub Desktop.
Save tony0x59/6871baa40c1cae39b0fd2b470e5ff418 to your computer and use it in GitHub Desktop.
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