• AVR Freaks

ascii - math conversion

Author
verminsky
Super Member
  • Total Posts : 274
  • Reward points : 0
  • Joined: 2003/11/07 12:45:55
  • Location: Forest Knolls, CA
  • Status: offline
2005/01/07 12:44:25 (permalink)
0

ascii - math conversion

Does anyone know if the C30 Compiler contains built-in functions to convert unsigned int to ascii? Or for that matter ANY number to ascii conversions. I found atoi but nothing in reverse.

Thanks

jc
#1

7 Replies Related Threads

    dcomer
    Super Member
    • Total Posts : 184
    • Reward points : 0
    • Joined: 2003/11/07 12:39:52
    • Status: offline
    RE: ascii - math conversion 2005/01/07 13:01:43 (permalink)
    0
    Part of the standard C library includes the functions atoi (alpha to integer) atof, etc. You need to include stdio.h in your file. You can also consider using sscanf. Both techniques may bloat your code.
    < Message edited by dcomer -- Jan. 7, 2005 1:03:24 PM >
    #2
    verminsky
    Super Member
    • Total Posts : 274
    • Reward points : 0
    • Joined: 2003/11/07 12:45:55
    • Location: Forest Knolls, CA
    • Status: offline
    RE: ascii - math conversion 2005/01/07 13:12:40 (permalink)
    0
    Thanks - but I knew that. What I'm trying to determine is if there is a "standard" (in Microchip C30) function to do the reverse - int-to-ascii or unsigned int or long to ascii. Any ideas are more than welcome.

    jc
    #3
    cawilkie
    Administrator
    • Total Posts : 1977
    • Reward points : 0
    • Joined: 2003/11/07 12:49:11
    • Status: offline
    RE: ascii - math conversion 2005/01/07 15:14:31 (permalink)
    0
    JC,

    You could try using sprintf...

    main() {
    char buf[32];

    sprintf(buf,"%d",32);
    }

    This solution involves using the standard C library, however.

    Regards,
    Calum
    #4
    Guest
    Super Member
    • Total Posts : 80500
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: ascii - math conversion 2005/01/08 07:39:38 (permalink)
    0
    Hello,

    How about this solution?

    Reagards,

    Daniel


    /*******************************************************************************

    Name: itoa.c

    Author: Daniel F. Ramirez

    Date: 05/03/1995

    Description:

    This is a C test program which will test the integer to ascii
    conversion (itoa) function.

    *******************************************************************************/

    #include "17c42.h"
    //#include <stdio.h>

    #pragma option v

    #define TRUE 1
    #define FALSE 0

    long Value = 0;

    void reverse(char near * s);
    void itoa(char near * s);
    int strlen(char near * s);

    main()
    {

    char anum4[10];

    // Convert from decimal to ascii string

    Value = 0;
    itoa(anum4);
    Value = 12;
    itoa(anum4);
    Value = 123;
    itoa(anum4);
    Value = 1234;
    itoa(anum4);
    Value = 12345;
    itoa(anum4);

    Value = -1;
    itoa(anum4);
    Value = -12;
    itoa(anum4);
    Value = -123;
    itoa(anum4);
    Value = -12345;
    itoa(anum4);


    return (TRUE);
    }
    //******************************************************************************
    //* itoa - Convert a number from integer to an ascii string
    //******************************************************************************

    void itoa(char near * s)
    {
    char near *ps; // Pointer to start of
    // string s
    long Quotient = 0; // Quotient of integer
    // value and current
    // power of 10
    long Remainder = 0; // Remainder
    long Sign = 0; // Sign of integer value
    long TEN = 10; // The value 10

    // Save address to start of string

    ps = s;

    // Determine Sign of integer

    if (Value < 0)
    {
    Sign = -1;
    Value = -Value;
    } else
    {
    Sign = 1;
    }

    // Convert integer value to ascii string by succesively dividing by 10


    do
    {
    // Compute quotient to get next ascii digit

    Quotient = Value / TEN;

    // Compute remainder

    Remainder = Value - Quotient * TEN;

    // Convert digit from numeric to ascii and save digit in string

    *s = Remainder + 48;
    s++;

    // Set current value to remainder

    Value = Quotient;


    } while (Quotient != 0);

    // Insert sign if necessary

    if (Sign < 0)
    {
    *s = '-';
    s++;
    }
    // Insert string terminator

    *s = 0;

    // Restore address to start of string

    s = ps;

    // Reverse string

    reverse(s);

    }

    //******************************************************************************
    //* strlen - Determines length of string
    //******************************************************************************

    int strlen(char near * s)
    {
    int Length;
    char near *p;

    p = s;
    Length = 0;
    while (*s != 0)
    {
    Length++;
    s++;
    }

    s = p;
    return (Length);
    }

    //******************************************************************************
    //* reverse - Reverse the contents of string in place
    //******************************************************************************

    void reverse(char near * s)
    {
    char c;
    char near *pi;
    char near *pj;

    for (pi = s, pj = s + strlen(s) - 1; pi < pj; pi++, pj--)
    {
    c = *pi;
    *pi = *pj;
    *pj = c;
    }
    }

    #5
    Guest
    Super Member
    • Total Posts : 80500
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: ascii - math conversion 2005/01/08 11:05:26 (permalink)
    0
    Or


    void itoa(int i,char *s)
    {
    unsigned long l=100000;
    unsigned char j=0;

    if(i<0)
    {
    s[0]='-';
    j=1;
    i*=-1;
    }

    if(i==0)
    {
    s[0]='0';
    s[1]='\0';
    return;
    }

    while(l>i) l/=10;
    while(l)
    {
    s[j++]=(i/l)%10;
    l/=10;
    }
    s[j]='\0';
    return;
    }


    I think this should work, though I haven't tested it, so caveat emptor.

    Julian
    #6
    bob_barr
    Super Member
    • Total Posts : 5428
    • Reward points : 0
    • Joined: 2003/11/07 12:35:23
    • Location: Morgan Hill, CA
    • Status: offline
    RE: ascii - math conversion 2005/01/08 11:18:33 (permalink)
    0
    unsigned long l=100000;

    That single lower-case letter 'L' makes this code really tough to read. It's extremely difficult to distinguish the lower-case letter 'L' from the numeric digit '1'. In some fonts, they're almost identical in appearance.

    Aside from that, I think that there may be other problems with the two successive 'while' statements but I haven't thought through them in detail.

    While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others.
    Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
    #7
    verminsky
    Super Member
    • Total Posts : 274
    • Reward points : 0
    • Joined: 2003/11/07 12:45:55
    • Location: Forest Knolls, CA
    • Status: offline
    RE: ascii - math conversion 2005/01/11 14:19:05 (permalink)
    0
    Thanks everyone.!

    It's dspicable
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5