PICmicro & dsPIC >> PICmicro & dsPIC

Pages: 1
thunderdan



Reged: Oct 16 2003
Posts: 4
Converting; int to char
      #6933 - Thu Jan 30 2003 05:22 PM

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!



Post Extras: Print Post   Remind Me!   Notify Moderator  
Dan HenryModerator
Guru
****

Reged: Oct 16 2003
Posts: 3838
Loc: Colorado
Re:Converting; int to char
      #6934 - Thu Jan 30 2003 05:33 PM

>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;
    }
}
 


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
4 registered and 39 anonymous users are browsing this forum.

Moderator:  mikerj, jtemples, jeff, Dan Henry, Andrew L 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 2013

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5