Tuesday, May 21, 2013

int AscToInt(const char *num) Function

int AscToInt(const char *num){
    int number = 0;
    bool neg = false;
    if(*num == '-'){
        neg = true;
        num++;
    }
    while(*num){
        number = number * 10 + (*num-48);
        num++;
    }
    if(neg){
        number *= -1;
    }
    return number;
}

1 comment:

  1. This solution seemed really short, so I wanted to try it. In fact, it doesn't return 0 when input contains not numerical data such as letters or symbols (e.g q123 is converted to 65123)

    ReplyDelete