Using 32bit Integers with Z88DK

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Zetr0
Member
Posts: 33
Joined: Mon Aug 22, 2011 12:38 pm

Using 32bit Integers with Z88DK

Post by Zetr0 »

Hello my fellow Z88DK'ers

So in the midst of optimising and writing some conversion functions I have discovered an issue - its most likely because I have been babied by 'C' compilers in the past but perhaps I am missing something... (don't ask the misses, she would agree... )


So because I want a smallest foot print in code as I can get I have *arbitrarily* limited myself to <stdio.h> one of the functions I need is an ascii to int function -

here is a simple bit of C code to do just that

Code: Select all

int ascii_to_int16( char *str )                              // this is limited to +65535 ( or -32758 / +32767 unsigned)
{
        int i=0;
        int num = 0;
    int number_of_digits = 0;
    int magnitude = 1;                                       // (starting at the end of the string)
                                                             // every left shift in the string we multiply by 10
        while( str[ i ] != '\0')                             // not including string.h - keep as much stdio as possible
        {                                                    // strlen would be nice but a quick dirty way to save a few
               i++;                                          // Kbytes by not including (string.h)
        }
        number_of_digits = i;                                // this includes the null byte terminator

        for (i = (number_of_digits - 1); i >= 0; i--)            // read from the far right of the string to the left
        {
                num += (str [i] - CHAR_TO_INT) * magnitude;          // -48 to the ascii value to derive the int value
                magnitude *= 10;                                     // the int value is multiplied by the magnitude
        }                                                        // is incresed (*10)
        return num;
}
For some of my projects I will need to use values that will exceed even an unregistered int.

I have tried using "long" prefix with int, but sadly I couldn't display a 32 bit word ... saying that it may of been the display routine... but I thought I would check to see if long words (32bit integers) are supported

Thanks for reading
Zetr0
Member
Posts: 33
Joined: Mon Aug 22, 2011 12:38 pm

Post by Zetr0 »

LOL, take it all back.... display routine.....
User avatar
dom
Well known member
Posts: 1314
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

Why not use sscanf() ? That's in stdio.h
Zetr0
Member
Posts: 33
Joined: Mon Aug 22, 2011 12:38 pm

Post by Zetr0 »

dom wrote:Why not use sscanf() ? That's in stdio.h
I suspect habitual programming techniques and not thinking outside the box ;)

thanks for the heads up on that sir =)
Post Reply