Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting char to an int (without using a standard function)

1 view
Skip to first unread message

Dann Corbit

unread,
Nov 1, 1999, 3:00:00 AM11/1/99
to
Allan Ashton <allana...@mywitsend99.freeserve.co.uk> wrote in message
news:381e300c...@news.freeserve.co.uk...
> Would anyone please tell me how I can do this. i.e.
>
>
> char * string;
> int integer1;
>
> // input string for numeric values only using scanf....
> ..........
>
> // set integer1 somehow to the value of string..

Your spec is rather incomplete.
The scanf() function has a conversion specifier for integers.
Else, you can go char by char for isdigit() and multiply by 10 each time.
Don't forget to allocate memory for string.
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. Newsgroup http://www.dejanews.com/~c_a_p
C.A.P. FAQ: ftp://38.168.214.175/pub/Chess%20Analysis%20Project%20FAQ.htm

Allan Ashton

unread,
Nov 2, 1999, 3:00:00 AM11/2/99
to
Would anyone please tell me how I can do this. i.e.


char * string;
int integer1;

// input string for numeric values only using scanf....
..........

// set integer1 somehow to the value of string..

Many thanks.

Al Bowers

unread,
Nov 2, 1999, 3:00:00 AM11/2/99
to

Dann Corbit wrote:

> Allan Ashton <allana...@mywitsend99.freeserve.co.uk> wrote in message
> news:381e300c...@news.freeserve.co.uk...

> Your spec is rather incomplete.
> The scanf() function has a conversion specifier for integers.
> Else, you can go char by char for isdigit() and multiply by 10 each time.
> Don't forget to allocate memory for string.

Yes, the spec is not clear, but I belive he wants to input a string of
numbers and convert to a type int without using an standard C functions to do
the conversion.

Here is a function, asctoint() that will do this. However, it is dangerous to
use on user input because the string may not convert to a valid int. You have
the hazard of int underflow or overflow. The string may not be a valid
number. etc.

#include <stdio.h>

int asctoint(const char *string) {
const char *s1,*s2,*num = "0123456789";
int sum = 0,negative = 0;

if(*string == '-'){
negative++;
string++;
}
for(s1 = string;*s1 != '\0';s1++) {
for(s2 = num;*s2 != '\0' ; s2++)
if(*s2 == *s1) break;
if(*s2 == '\0') return sum;
sum *= 10;
sum += s2 - num;
}
return negative?sum*-1:sum;
}

int main(void) {
char *number = "-1234";
printf("The string \"%s\" convert to int is %d\n",
number,asctoint(number));
return 0;
}

--
Al Bowers
Tampa, FL USA
abo...@combase.com
http://www.gate.net/~abowers/


Steven Huang

unread,
Nov 2, 1999, 3:00:00 AM11/2/99
to
Al Bowers (abo...@gate.net) wrote:
[...]

> Here is a function, asctoint() that will do this. However, it is dangerous to
> use on user input because the string may not convert to a valid int. You have
> the hazard of int underflow or overflow. The string may not be a valid
> number. etc.

> #include <stdio.h>

> int asctoint(const char *string) {
> const char *s1,*s2,*num = "0123456789";
> int sum = 0,negative = 0;

> if(*string == '-'){
> negative++;

This is stylistic, of course, but I personally don't like to set a
flag with an arithmetic operation. I prefer:

#define POSITIVE 1
#define NEGATIVE 2

int sign = POSITIVE;

sign = NEGATIVE;

> string++;
> }
> for(s1 = string;*s1 != '\0';s1++) {
> for(s2 = num;*s2 != '\0' ; s2++)
> if(*s2 == *s1) break;
> if(*s2 == '\0') return sum;

There's a bug here. If I entered "-1a", the function will return 1,
not -1.

> sum *= 10;
> sum += s2 - num;
> }

Why not just:

for (s1 = string; *s1; s1++)
if (*s1 >= '0' && *s1 <= '9')
{
sum *= 10;
sum += *s1 - '0';
}
else
{
return (sign == NEGATIVE) ? -sum : sum;
}

> return negative?sum*-1:sum;

Or simpler yet,

... ? -sum : sum;

Might even be faster on truly braindead compilers.

> }
[...]

Kien Ha

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to

There is a bug here too :-). For strings such as "-1", "-12345" or
"12345",
the else clause will never be reached and the for loop ends when *s1 ==
'\0';
So pull the return statement out of the for loop.

--
Kien

Steven Huang

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to
Kien Ha (Kie...@Mitel.COM) wrote:
[...]

> There is a bug here too :-).

Nice try, but no dice. :)

> For strings such as "-1", "-12345" or
> "12345", the else clause will never be reached and the for loop ends
> when *s1 == '\0';
> So pull the return statement out of the for loop.

You should've read further down, where I wrote:

> return negative?sum*-1:sum;

Or simpler yet,

... ? -sum : sum;

The return wasn't missing... from my mind, at least. ;)

Allan

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to
On 3 Nov 1999 16:16:25 GMT, sth...@hns.com (Steven Huang) wrote:
Cheers boys. That's been very useful!
0 new messages