Thread overview
cast issue
Aug 19, 2002
ben
Aug 19, 2002
user
Aug 19, 2002
ben
August 19, 2002
Hello Everybody

I hope stupid questions are welcome here.. I can't figure this out. I am trying to figure out how to convert chars to integers..

char *script = "0";
int number = (char) *script;

This returns the ascii number. (48 i think).
I need the number can someone help..

Also how do you do it with bigger numbers.

char *string[] = "50";

Something like that..

Ben


August 19, 2002
ben wrote:
> Hello Everybody
> 
> I hope stupid questions are welcome here.. I can't figure this out. I am trying to figure out how to convert chars to integers..
> 
> char *script = "0";
> int number = (char) *script;
> 
> This returns the ascii number. (48 i think).
> I need the number can someone help..
> 
> Also how do you do it with bigger numbers.
> 
> char *string[] = "50";
> 
> Something like that.. 
> 
> Ben
> 
>  

atoi is your friend.

Manual example
Header: stdlib.h

Prototype

int atoi(const char *nptr);

Description

The atoi function converts the string pointed to by nptr to an integer. The string may have leading spaces, tabs, and + or -. Conversion stops on the first unrecognized character. If there are no recognized characters, the result is 0.

Return Value

Returns the integer value derived from converting the string. Zero is returned if the input string has no recognizable characters.

Compatibility

DOS, Windows 3.x, Phar Lap, DOSX, Win32

See Also

atof, atol, _atold, _ecvt, _fcvt, scanf, strtol

Example

/*    Example of atoi  */
#include <stdio.h>
#include <stdlib.h>

void main()
{
   int result;
   char *test1 = "310";
   char *test2 = "No Number";

   result = atoi (test1);
   printf ("Test1 is %d\n", result);

   result = atoi (test2);
   printf ("Test2 is %d\n", result);
}


Output

Test1 is 310
Test2 is 0

August 19, 2002
Thank you so much..

Ben

user@domain.invalid wrote:

> ben wrote:
>> Hello Everybody
>> 
>> I hope stupid questions are welcome here.. I can't figure this out. I am trying to figure out how to convert chars to integers..
>> 
>> char *script = "0";
>> int number = (char) *script;
>> 
>> This returns the ascii number. (48 i think).
>> I need the number can someone help..
>> 
>> Also how do you do it with bigger numbers.
>> 
>> char *string[] = "50";
>> 
>> Something like that..
>> 
>> Ben
>> 
>> 
> 
> atoi is your friend.
> 
> Manual example
> Header: stdlib.h
> 
> Prototype
> 
> int atoi(const char *nptr);
> 
> Description
> 
> The atoi function converts the string pointed to by nptr to an integer. The string may have leading spaces, tabs, and + or -. Conversion stops on the first unrecognized character. If there are no recognized characters, the result is 0.
> 
> Return Value
> 
> Returns the integer value derived from converting the string.
> Zero is returned if the input string has no recognizable characters.
> 
> Compatibility
> 
> DOS, Windows 3.x, Phar Lap, DOSX, Win32
> 
> See Also
> 
> atof, atol, _atold, _ecvt, _fcvt, scanf, strtol
> 
> Example
> 
> /*    Example of atoi  */
> #include <stdio.h>
> #include <stdlib.h>
> 
> void main()
> {
>     int result;
>     char *test1 = "310";
>     char *test2 = "No Number";
> 
>     result = atoi (test1);
>     printf ("Test1 is %d\n", result);
> 
>     result = atoi (test2);
>     printf ("Test2 is %d\n", result);
> }
> 
> 
> Output
> 
> Test1 is 310
> Test2 is 0