www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - cast issue

↑ ↓ ← ben <zander echotech.ca> writes:
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

 
Aug 19 2002
↑ ↓ user domain.invalid writes:
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
Aug 19 2002
↑ ↓ → ben <zander echotech.ca> writes:
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

Aug 19 2002