Skip to content

Instantly share code, notes, and snippets.

@AlexisTM
Last active September 5, 2018 06:55
Show Gist options
  • Save AlexisTM/a8df86e96ba9d7eeda5b7f0df54c8614 to your computer and use it in GitHub Desktop.
Save AlexisTM/a8df86e96ba9d7eeda5b7f0df54c8614 to your computer and use it in GitHub Desktop.
The clever integer to ASCII hexadecimal conversion !
#include <stdio.h>
char *itohex(int n) {
int i = 2*sizeof (int);
char *str = malloc(i + 1);
str[i] = '\0';
for ( ; i > 0; n >>= 4)
str[--i] = "0123456789ABCDEF"[n&0xF];
return str;
}
int main()
{
char * data = itohex(256);
printf(data);
free(data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment