Thursday, May 30, 2013

ascToInt



Function which receives a char string of numbers (optional '+' or '-' in front) and returns the string as an int.

Assumes the user provides a valid string of numbers.

int ascToInt(const char *num) {
  int result = 0;
  int posOrNeg = 1;
  if ((*num == '+') || ((*num == '-') && (posOrNeg = -1)))
    num++;
  while (*num) {
    result = (result * 10) + (*num - '0');
    num++;
  }
  return result * posOrNeg;
}

No comments:

Post a Comment