473,378 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Converting strings into integers

1
Hi,

I'm a newbie in the matter of C++.
Now i'm trying to convert a text from a string (like str = "123") into an int-variable.
In Java i've used parseInt for this function, but i can't find the C++-equivalence.
I rather use a string and not a char-array...

Idea someone?

Thx in advance,
Misko
Aug 11 '07 #1
3 1957
Meetee
931 Expert Mod 512MB
Hi,

I'm a newbie in the matter of C++.
Now i'm trying to convert a text from a string (like str = "123") into an int-variable.
In Java i've used parseInt for this function, but i can't find the C++-equivalence.
I rather use a string and not a char-array...

Idea someone?

Thx in advance,
Misko
Hi Misko,

You can use atoi function for that.
Usage is like this:

int value = atoi ( const char * str );

This requires char array as const char * is passed as a parameter.

Regards
Aug 11 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
atoi() is deprecated in C++.

Use a stringstream.
Expand|Select|Wrap|Line Numbers
  1. int result;
  2. string str("123");
  3. stringstream ss;
  4. ss << str;
  5. ss >> result;
  6.  
Aug 11 '07 #3
I was just about to suggest a custom-made function, but the other ones look far better. I was going to do something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int sum = 0;
  3. int length = strlen(str);
  4. for (i = length - 1; i >= 0; i--)
  5.     sum += str[i] * pow(10, length - i);
  6.  
  7.  
I did not run the code, so it might have a bug or two, but you get the idea.
Aug 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Peter Warder | last post by:
I'm still struggling with these conversions! The wider code is working well but I can't get the compiler to accept my attempts at converting Integers (back) into Strings. What's the trick? ...
9
by: Hugo Amselschlag | last post by:
Hi there, I've implemented a local system hook to suppress certain windows beeing displayed by the axWebbrowser control. Now I need some more information before I can decide, whether to suppress...
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
4
by: fatboySudsy | last post by:
hi there, I have been struggling for a while with a client/server program. I have to authenticate in order to log on correctly. I have managed to get most things sorted, asking for username (...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
1
by: jimmypringles | last post by:
Anyone knows how how to add "number" strings without converting them to integers? I came up with this function but it didn't work perfectly. e.g. I added "1998" and "8991" and the result was...
3
by: Jeff | last post by:
....still new to vb.net 2005 I understand the concept of arrays, and have used them in other languages, but was hoping that someone could get me started with something. I have a fairly long...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
2
by: HONOREDANCESTOR | last post by:
I've been converting a dotnet dll to a com object so that it can be called from vb6. If I want to pass a structure to a routine in the com object, like this: Call MyRoutine(byref MyStruct as...
6
by: andrews | last post by:
I try to add strings with fixed length who are representing integers So I have to put spaces (I guesse) before short strings(from short integers). F.e. 12345 1 567 5432 54328
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.