"Code never lies, comments sometimes do." ~Ron Jeffries

The Ascii Conversion Challenge

For an small update on this code, look here!

Given the challenge to make a function for converting arbitrary C strings into an integer, I first went for the most simple solution devoid of error checking. Overlooking the possibility of negative integers also made it easy enough to get a prototype to manage valid, positive input by working backwards through the string and summing the value of each element multiplied by the exponent of 10 to the power of 1, then 2, etc.

——————————————————-

int exponential( int base, int exp ){
	int res;
	int i = 1;

	switch( exp ){
	case 0:
		res = 1;
		break;
	default:
		res = base;
	}

	for( i; i < exp; i++, res *= base );
	return res;
}

int AscToInt( const char* str ){
	int lastI;
	int i;
	int res;
	// Get last addressable index
	for( lastI = 0; str[lastI+1]; lastI++ );
	// Push digits to result
	for( i = lastI; i >= 0; res += (str[i] - '0') * exponential( 10, lastI - i ) , i-- );

	return res;
}

——————————————————-

With the majority of functionality covered, now all that’s needed is a strategic insert of some catches for bad input…

——————————————————-

int AscToInt( const char* str ){
	int lastI;
	int i = 0;
	int res=0;
	// Get last addressable index and search for non-NULL, non numeric characters
	for( lastI = 0; str[lastI+1] &amp;&amp; !((str[lastI+1] &lt; 48 || str[lastI+1] &gt; 57) &amp;&amp; (res=1)); lastI++ );
	// Push digits to result
	switch( res ){
	case 1:
		return 0;
		break;
	default:
		for( i = lastI; i &gt;= 0; res += (str[i] - '0') * exponential( 10, lastI - i ) , i-- );
		return res;
	}
}

——————————————————-

I used res as a flag to mark a bad input character, but got stuck here for a time until I realized that in my original write up of this the flag would get set only if the test condition if the for loop were true! This meant that if there was a faulty character at the end of the array the flag would get set but the loop would continue, and if there was a valid character at the end the flag wouldn’t get set so the loop couldn’t continue! This was easily fixed after diagnosis with a NOT before evaluating if there’s a faulty character and setting the flag. Now all evaluations will still be completed as desired and, if need be, set the flag, but will continue on good input and not continue on bad input.

Also, as one needs to with the use of a flag variable, I altered the return to switch on the result and return 0 or calculate and return the integer as needed.

For one last addition, support for negative numbers! I will assume any negative numbers will be signified by a dash character in the first spot of the string.

——————————————————-

int AscToInt( const char* str ){
	int lastI;
	int i = 0;
	int res=0;
	int neg=0;
	
	switch ( str[0] ){
	case '-':
		neg = 1;
		i = 1;
	}
	
	// Get last addressable index and search for non-NULL, non numeric characters
	for( lastI = i; str[lastI+1] && !((str[lastI+1] < 48 || str[lastI+1] > 57) && (res=1)); lastI++ );
	// Push digits to result
	switch( res ){
	case 1:
		return 0;
		break;
	default:
		for( i = lastI; i >= neg; res += (str[i] - '0') * exponential( 10, lastI - i ) , i-- );
		switch( neg ){
		case 1:
			res = 0 - res;
		default:
			return res;
		}
	}
}

——————————————————-

I added another flag, neg, to signal a negative input with 1 and otherwise be 0, and used it both to affect whether str[0] is evaluated when the integer is being contructed and to switch on before return to allow negative inputs to become negative.

One response

  1. Shehzin Arshad

    I was a little unsure in regards to calculating exponential values without using the pow function and thus gave it very little thought. Nice to see a different method of calculating those values. Much appreciated. Thanks!

    2013-05-21 at 22:45

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s