>It's been a while since I've posted on this forum, but it's been a while since I've had to program a PIC chip! This Question relates to the humble old 16F84...
>
>Is there an easy way to convert a type int to a type char? without using the printf() or
>sprintf() function?
>
>I've got a global int called RPM and I need to display that on an LCD; trouble is, my LCD rutine only accepts a string a chars! The fantastic tech staff at Hi-Tech gave me this e.g.
>
>"You can use an int (16-Bit) as a char (8-Bit) by casting it. For example:
>int var1 = 0x1234;
>char var2;
>var2 = (char) var1; // cast an int as a char"
>
>But If I have 1520d stored in RPM I need to end up with 4 chars, '1' '5' '2' and '0' (all in decimal, I would realy need the ASCII e-q's but do you get the general drift??) Of course if the code that Hi-Tech supplied is correct, I don't really understand it so any help would be fantastic! Thanks Daniel!
>
Could you get by with adapting either of the following?
typedef unsigned char U8;
typedef signed long S32;
typedef unsigned long U32;
/* NAME: SlToStr
* PURPOSE: Convert signed long to decimal string.
*
* SYNOPSIS: void SlToStr( char *s, S32 bin, U8 n );
*
* DESCRIPTION:
*
* The function stores a NUL-terminated string, in "n" + 1 (plus 1 for
* the NUL string terminator) successive elements of the array whose
* first element has the address "s", by converting "bin" to a sign
* character followed by "n" - 1 decimal characters.
*/
void SlToStr( char *s, S32 bin, U8 n )
{
if (bin >= 0)
*s = '+';
else
{
*s = '-';
bin = -bin;
}
s += n;
*s = '\0';
while (--n)
{
*--s = (bin % 10) + '0';
bin /= 10;
}
}
/* NAME: UlToStr
* PURPOSE: Convert unsigned long to decimal string.
*
* SYNOPSIS: void UlToStr( char *s, U32 bin, U8 n );
*
* DESCRIPTION:
*
* The function stores a NUL-terminated string, in "n" + 1 (plus 1 for
* the NUL string terminator) successive elements of the array whose
* first element has the address "s", by converting "bin" to "n" decimal
* characters.
*/
void UlToStr( char *s, U32 bin, U8 n )
{
s += n;
*s = '\0';
while (n--)
{
*--s = (bin % 10) + '0';
bin /= 10;
}
}