Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top