> -----Original Message-----
> From: Gene_Gaunt@xxxxxxxxxxxxxxx [mailto:Gene_Gaunt@xxxxxxxxxxxxxxx]
> Subject: [C400-L] add long strings as unsigned integers
> 
> How would you code addition of arbitrarily wide strings in C language?
> For example
> 
>    x'88 00 00 00 88 00 00 00 00 22 00 00 00 33 00 00 E4'
> +  x'88 00 00 00 F1 00 11 00 11 00 22 00 22 00 22 00 33'
>    ------------------------------------------------------
> =  x'10 00 00 01 79 00 11 00 11 22 22 00 22 33 22 01 17'

Hope I'm not late to the party - back from a great connectionless vacation :)

Here's a generic string addition function (culled from an old CS course 
assignment we were given back in the dark ages) that should work for any number 
base, and any length of strings (even mismatched lengths). I noticed that you 
are ignoring the msb carry (if it results in one) to achieve the same results, 
in the addStringsAsNumbers function just adjust the allocation for result from;
        result = (char *)realloc( result, largest + 2 );
to;
        result = (char *)realloc( result, largest + 1 );

And remove the memcpy() just before the return statement.

If you've already solved your problem, then I hope the code will be of 
assistance to any 'newbies' on the list you were wondering 'how to do it'.

Thanks,

--phil

#include "string.h"
#include "stdlib.h"

char * addStringsAsNumbers( char one[], char two[], int radix, char result[] );

char lookUp[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main(int argc, char* argv[])
{
        char sH1[] = "88000000880000000022000000330000E4";
        char sH2[] = "88000000F1001100110022002200220033";
        char sH3[] =         "880000000022000000330000E4";
        char sH4[] = "88000000F1001100110022002200220033";
        char sH5[] =         "880000000022000000330000E4";
        char sH6[] = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
        char sH7[] = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
        char sH8[] = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

        char *r  = NULL;
        
        r = (char *)realloc( r, 1 );
        r = addStringsAsNumbers( sH1, sH2, 16, r );
        printf( "   %*s\n", strlen(r), sH1 );
        printf( "+  %*s\n", strlen(r), sH2 );
        printf( "=  %*s\n\n", strlen(r), r );

        r = (char *)realloc( r, 1 );
        r = addStringsAsNumbers( sH3, sH4, 16, r );
        printf( "   %*s\n", strlen(r), sH3 );
        printf( "+  %*s\n", strlen(r), sH4 );
        printf( "=  %*s\n\n", strlen(r), r );

        r = (char *)realloc( r, 1 );
        r = addStringsAsNumbers( sH5, sH6, 16, r );
        printf( "   %*s\n", strlen(r), sH5 );
        printf( "+  %*s\n", strlen(r), sH6 );
        printf( "=  %*s\n\n", strlen(r), r );

        r = (char *)realloc( r, 1 );
        r = addStringsAsNumbers( sH7, sH8, 16, r );
        printf( "   %*s\n", strlen(r), sH7 );
        printf( "+  %*s\n", strlen(r), sH8 );
        printf( "=  %*s\n\n", strlen(r), r );

        return 0;
}

char * addStringsAsNumbers( char one[], char two[], int radix, char *result )
{
        int lenOne = strlen( one );
        int lenTwo = strlen( two );
        int largest = (lenOne >= lenTwo) ? lenOne : lenTwo;
        int carry = 0;

        result = (char *)realloc( result, largest + 2 );
        memset( result, '\0', largest + 2 );

        /*-- copy into like size arrays --*/
        char *p1 = (char *)malloc( largest );
        memcpy( &p1[largest-lenOne], one, lenOne );
        char *p2 = (char *)malloc( largest );
        memcpy( &p2[largest-lenTwo], two, lenTwo );

        /*-- starting with the lsb, ending with the msb --*/
        while( largest > 0 )
        {
                char *cp1 = (char *)calloc(2, 0); 
                memcpy( &cp1[0], &p1[largest-1], 1);
                char *cp2 = (char *)malloc(2); 
                memcpy( &cp2[0], &p2[largest-1], 1);

                /*-- get the parm one current value--*/
                long np1 = strtol( cp1, NULL, radix );
                if( np1 > radix )
                {
                        fprintf( stderr, "Parameter one contains values greater 
than the base." );
                        return NULL;
                }

                /*-- get the parm two current value--*/
                long np2 = strtol( cp2, NULL, radix );
                if( np2 > radix )
                {
                        fprintf( stderr, "Parameter two contains values greater 
than the base." );
                        return NULL;
                }

                /*-- add the individual values --*/
                int res = np1 + np2 + carry;

                /*-- check for overflow --*/
                carry = ( res >= radix ) ? 1 : 0;
                res = ( res >= radix ) ? res - radix : res;

                /*-- set value in result, and move to next--*/
                memcpy( &result[largest], &lookUp[res], 1) ;
                largest--;
        }

        /*-- there may have been a msb carry --*/
        memcpy( &result[0], &lookUp[carry], 1 );
        return result;
}


This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].