A89: Re: ti-89 + TI-GCC vector question


[Prev][Next][Index][Thread]

A89: Re: ti-89 + TI-GCC vector question




> Ack, it's been awhile, but does anyone know how to take a int and change
it
> into a char pointer so i can disp it.

I think that's the purpose of printf, but since I take a masochistic
pleasure in writing recursive functions, try this.  Be forwarned that it's
untested.  Niklas and others, PLEASE check my C code here - I'm still
working on it =)



// -------------------------------------------------------------------------
--
// Int to String routines
// by Scott Noveck (scott@acz.org)
// These do NOT yet handle negative numbers and are NOT portable
// although that can be fixed easily if necessary.
// Two versions follow below - inttostring gives only used digits
// inttostring_fixed gives a fixed number of digits

#define DIGIT_START = 0x10 // ASCII code where digits start (not portable!)
#define STORAGE_SIZE = 5   // Width of longest possible string (int = 5)

// function prototypes
char *inttostring(int num, char *storage);
char *inttostring_fixed(int num, int digits, char *storage);
char *inttostring_recurse(int num, char *storage);

char storage[STORAGE_SIZE+1];
// this is where we will store the string
// use "storage" w/o quotes as parameter when calling funcs

// inttostring
// wrapper func that starts recursion using point to LAST byte of storage
// this version will not give leading zeros
// fixed width version follows below
// returns pointer to string
char *inttostring(int num, char *storage)
{
    return(inttostring_recurse(num,storage+STORAGE_SIZE));
}

// inttostring_fixed
// wrapper func that starts recursion using point to LAST byte of storafe
// this version always gives specified number of digits
// returns pointer to string
char *inttostring_fixed(int num, int digits, char *storage)
{
    inttostring_recurse(num,storage+STORAGE_SIZE);
    return(storage+STORAGE_SIZE-digits);
}

// inttostring_recurse
// simple recursive routine to get string
char *inttostring_recurse(int num,char *storage)
{
    if (num==0) {
        return(storage);
    } else {
        *storage = num % 10 + DIGIT_START;
        return(inttostring_recurse(num/10,storage-1));
    }
}
// -------------------------------------------------------------------------
--


//    -Scott



Follow-Ups: