Last active
September 5, 2018 06:55
-
-
Save AlexisTM/a8df86e96ba9d7eeda5b7f0df54c8614 to your computer and use it in GitHub Desktop.
The clever integer to ASCII hexadecimal conversion !
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 <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