Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

converting from char to int

G'day,
I am in need of a method of converting a string to an integer.
Thanks,
Andrew
0
galdes
Asked:
galdes
1 Solution
 
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
 
decornezCommented:
For the purpose of locking.
Agree with snoegler & chensu.
can also use atof() to convert to double.
Cheers.
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now