• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • 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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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