converting from char to int

Posted on 1998-08-31
Medium Priority
Last Modified: 2010-04-02
I am in need of a method of converting a string to an integer.
Question by:galdes
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1171776
use atoi() (ascii to integer)
or atol(ascii to long)

"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 :)
LVL 23

Expert Comment

ID: 1171777
sscanf is also able to do so.

Accepted Solution

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

Expert Comment

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

Viktor Ivanov
LVL 84

Expert Comment

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)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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