Skip to content

Commit

Permalink
reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
dlubarov committed Jun 3, 2011
1 parent 77d04e7 commit a8f3bb0
Show file tree
Hide file tree
Showing 3 changed files with 1,735 additions and 0 deletions.
43 changes: 43 additions & 0 deletions itoa.c
@@ -0,0 +1,43 @@
/* Convert the integer D to a string and save the string in BUF. If
BASE is equal to 'd', interpret that D is decimal, and if BASE is
equal to 'x', interpret that D is hexadecimal. */
void itoa(char *buf, int base, int d)
{
char *p = buf;
char *p1, *p2;
unsigned long ud = d;
int divisor = 10;

/* If %d is specified and D is minus, put `-' in the head. */
if (base == 'd' && d < 0)
{
*p++ = '-';
++buf;
ud = -d;
}
else if (base == 'x')
divisor = 16;

/* Divide UD by DIVISOR until UD == 0. */
do
{
int remainder = ud % divisor;
*p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
}
while (ud /= divisor);

/* Terminate BUF. */
*p = 0;

/* Reverse BUF. */
p1 = buf;
p2 = p - 1;
while (p1 < p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
}

0 comments on commit a8f3bb0

Please sign in to comment.