Skip to content

Instantly share code, notes, and snippets.

@ruda
Created November 3, 2017 01:01
Show Gist options
  • Save ruda/5d768604bbc0a02c866c502b4d33592a to your computer and use it in GitHub Desktop.
Save ruda/5d768604bbc0a02c866c502b4d33592a to your computer and use it in GitHub Desktop.
/*
* From http://c-faq.com/misc/hexio.html
* modified for padding zeros - ruda
*/
#define NULL 0
char *
baseconv(unsigned int num, int base)
{
static char retbuf[8+1];
char *p;
if(base < 2 || base > 16)
return NULL;
p = &retbuf[sizeof(retbuf)-1];
*p = '\0';
do {
*--p = "0123456789abcdef"[num % base];
num /= base;
} while(num != 0);
while (p != retbuf)
*--p = '0';
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment