Solved

converting from char to int

Posted on 1998-08-31
5
190 Views
Last Modified: 2010-04-02
G'day,
I am in need of a method of converting a string to an integer.
Thanks,
Andrew
0
Comment
Question by:galdes
5 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1171776
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
 
LVL 23

Expert Comment

by:chensu
ID: 1171777
sscanf is also able to do so.
0
 

Accepted Solution

by:
decornez earned 20 total points
ID: 1171778
For the purpose of locking.
Agree with snoegler & chensu.
can also use atof() to convert to double.
Cheers.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1171779
If you are using Borland C++ use StrToInt("12");

Regards,
Viktor Ivanov
0
 
LVL 84

Expert Comment

by:ozo
ID: 1171780
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now