We help IT Professionals succeed at work.

converting from char to int

galdes
galdes asked
on
Medium Priority
220 Views
Last Modified: 2010-04-02
G'day,
I am in need of a method of converting a string to an integer.
Thanks,
Andrew
Comment
Watch Question

Commented:
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 :)

Commented:
sscanf is also able to do so.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
If you are using Borland C++ use StrToInt("12");

Regards,
Viktor Ivanov
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.