converting from char to int

G'day,
I am in need of a method of converting a string to an integer.
Thanks,
Andrew
galdesAsked:
Who is Participating?
 
decornezCommented:
For the purpose of locking.
Agree with snoegler & chensu.
can also use atof() to convert to double.
Cheers.
0
 
snoeglerCommented:
use atoi() (ascii to integer)
or atol(ascii to long)

Remember:
"1234" -> 1234
"12.34"-> 12
"-1234"-> -1234
"-12.34"-> -12
"12.75"-> 12
"-12.75"-> -12

And make sure that there are no leading spaces - as far as i know it is compiler dependent
if they are omitted or if simply '0' is returned.
I'll post this as a comment as i don't know if this answers your question - but just let me know
it if i should post an answer :)
0
 
chensuCommented:
sscanf is also able to do so.
0
 
viktornetCommented:
If you are using Borland C++ use StrToInt("12");

Regards,
Viktor Ivanov
0
 
ozoCommented:
decornez, what's the purpose of locking just to agree with snoegler & chensu?

(strlen and strtol also convert strings to intgers,
if the integers returned by atoi or sscanf don't happen to me the ones you want)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.