Itoa for 8051 based micros

Status
Not open for further replies.

phaedrus

Member level 4
Joined
Feb 20, 2007
Messages
78
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,913
keil itoa

Has anyone come across a C "itoa" kind of implementation for 8051 based micros ? The printf and sprintf are far too heavy for the smaller micros.
 

itoa keil

/* reverse: reverse string s in place */
void reverse(char s[])
{
int c, i, j;

for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s;
s = s[j];
s[j] = c;
}
}


/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;

if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s = '\0';
reverse(s);
}
 

c51 itoa

An implementation with less storage and code requirements (according to Keil C51), also suited for direct output through putc() instead of using strings:
Code:
void itoa(int n, char *s) { 
char i;
int n1;
if (n<0) {
  n=-n;
  *s++='-';
}
do
{
  n1=n;
  i=0;
while (1) {
  if (n1<=9)   {
    *s++=n1+'0';
    break;
  }
  n1=n1/10;
  i++;
}
while (i) {
  i--;
  n1=n1*10;
}
n-=n1;
}while (n);
*s++=0;
}
An assembler version could farther reduce the resource usage.
 

8051 itoa

Hello ,

Thanks for the code snippets. I will need to do some work on formatting the o/p to get exactly what I need viz. to get some kind of a float point display.This however is more than enough to get started with.

Thanks

Hi FvM, I thought you were only into analog design,thanks for your input as usual.
 

itoa 8051
 

    phaedrus

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…