Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
converting char to an int (without using a standard function)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dann Corbit  
View profile  
 More options Nov 1 1999, 3:00 am
Newsgroups: comp.lang.c
From: "Dann Corbit" <dcor...@solutionsiq.com>
Date: 1999/11/01
Subject: Re: converting char to an int (without using a standard function)
Allan Ashton <allanasht...@mywitsend99.freeserve.co.uk> wrote in message

news:381e300c.14153455@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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Allan Ashton  
View profile  
 More options Nov 2 1999, 3:00 am
Newsgroups: comp.lang.c
From: allanasht...@mywitsend99.freeserve.co.uk (Allan Ashton)
Date: 1999/11/02
Subject: converting char to an int (without using a standard function)
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Al Bowers  
View profile  
 More options Nov 2 1999, 3:00 am
Newsgroups: comp.lang.c
From: Al Bowers <abow...@gate.net>
Date: 1999/11/02
Subject: Re: converting char to an int (without using a standard function)

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
abow...@combase.com
http://www.gate.net/~abowers/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Huang  
View profile  
 More options Nov 2 1999, 3:00 am
Newsgroups: comp.lang.c
From: sthu...@hns.com (Steven Huang)
Date: 1999/11/02
Subject: Re: converting char to an int (without using a standard function)
Al Bowers (abow...@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.

>    }

[...]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kien Ha  
View profile  
 More options Nov 3 1999, 3:00 am
Newsgroups: comp.lang.c
From: Kien Ha <Kien...@Mitel.COM>
Date: 1999/11/03
Subject: Re: converting char to an int (without using a standard function)

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Huang  
View profile  
 More options Nov 3 1999, 3:00 am
Newsgroups: comp.lang.c
From: sthu...@hns.com (Steven Huang)
Date: 1999/11/03
Subject: Re: converting char to an int (without using a standard function)
Kien Ha (Kien...@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.  ;)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Allan  
View profile  
 More options Nov 3 1999, 3:00 am
Newsgroups: comp.lang.c
From: A...@mywitsend99.freeserve.co.uk (Allan)
Date: 1999/11/03
Subject: Re: converting char to an int (without using a standard function)
On 3 Nov 1999 16:16:25 GMT, sthu...@hns.com (Steven Huang) wrote:
Cheers boys. That's been very useful!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »