Discussion:
[Sdcc-user] PIC14 itoa, sprintf functions
Steve Brown
2006-03-23 01:42:04 UTC
Permalink
Hi all,

I'm still getting to grips with the PIC14 port of SDCC, so forgive me if
the answer are obvious; they aren't to me ;-)

Needing an itoa or similar function to convert from an int (or whatever)
to a string, I went to code the obvious function using sprintf, but
that function isn't available.

Can somebody suggest how I might implement such a function?

-S
Julian Guarin
2006-03-23 02:20:05 UTC
Permalink
int value=-531;
int sign;
char buffer[10];
int index=0;
int auxiliar;
if (value<0)
sign=1;
if (sign){
buffer[index]='-';
printf ("%c\n",buffer[index]);
index++;
value=value*-1;
}

do{
aux=value;
while (value>=10)
value/=10;
buffer[index]=value;

/* Not necessary */
printf("%c\n",buffer[index]);

index++;
value=aux%10;

}while(aux>10);
printf("%s\n",buffer);


The output is:
-
5
3
1
-531
Post by Steve Brown
Hi all,
I'm still getting to grips with the PIC14 port of
SDCC, so forgive me if
the answer are obvious; they aren't to me ;-)
Needing an itoa or similar function to convert from
an int (or whatever)
to a string, I went to code the obvious function
using sprintf, but
that function isn't available.
Can somebody suggest how I might implement such a
function?
-S
-------------------------------------------------------
Post by Steve Brown
This SF.Net email is sponsored by xPML, a
groundbreaking scripting language
that extends applications into web and mobile media.
Attend the live webcast
and join the prime developer group breaking into
this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
Post by Steve Brown
_______________________________________________
Sdcc-user mailing list
https://lists.sourceforge.net/lists/listinfo/sdcc-user
______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com
Ken Jackson
2006-03-23 10:48:04 UTC
Permalink
If you want to convert a 16-bit binary value into text, you should
check out these two links. They are two versions of the same
algorithm written by John Payson and documented by Scott Dattalo:

<http://www.dattalo.com/technical/software/pic/bcd.txt> and
<http://www.piclist.com/techref/microchip/math/radix/b2bu-16b5d.htm>

I originally wrote a straight-forward function that used division
and modulo, but it took too much time so I searched and found
this. It's some of the most optimized PIC code I've seen.

It actually produces BCD, but of course you just add the ASCII '0'
character to each byte to get ASCII.

Also note that the base link has lots of useful PIC code:
<http://www.piclist.com/techref/microchip/>

-Ken Jackson
Post by Steve Brown
Hi all,
I'm still getting to grips with the PIC14 port of SDCC, so forgive me if
the answer are obvious; they aren't to me ;-)
Needing an itoa or similar function to convert from an int (or whatever)
to a string, I went to code the obvious function using sprintf, but
that function isn't available.
Can somebody suggest how I might implement such a function?
-S
Loading...