All pastes #24965 Raw Edit

Anonymous

public text v1 · immutable
#24965 ·published 2005-10-08 20:59 UTC
rendered paste body
int my_atoi(const char *str, unsigned int base) {

	
	if(base > 36) return ATOI_ERROR_BASE;
	if(str == 0) return ATOI_ERROR_NODIGIT;

	unsigned int result = 0;
	const char *cursor, *low;
	
	cursor = str;
	for(;*cursor != '\0'; cursor++);
	cursor--;
	low = cursor;

	while(cursor >= str) 
	{

		if(*cursor >= 48 && (base > 10?*cursor <=57:*cursor <= (48 + base))) 
			result += (*cursor - 48) * pow(base,(low - cursor));
		else if(base > 10 && (*cursor >= 65 && *cursor < (65 + base - 10)))
			result += (*cursor - 65 + 10) * pow(base,(low - cursor));
		else return ATOI_INVALID_DIGIT;
	
	cursor--;
	}

	return result;

}