# Simple Way To Convert A String To An Integer

Page 1 of 1

## 4 Replies - 969 Views - Last Post: 24 February 2009 - 05:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=88827&amp;s=a8f56554f8ff8302206e466e95449db2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 programble

Reputation: 51
• Posts: 1,315
• Joined: 21-February 09

# Simple Way To Convert A String To An Integer

Posted 24 February 2009 - 04:06 PM

How can I...

1. Figure out if a string is a number?
2. Convert that string to an integer?
Is This A Good Question/Topic? 0

## Replies To: Simple Way To Convert A String To An Integer

### #2 alf

Reputation: 0
• Posts: 11
• Joined: 22-February 09

## Re: Simple Way To Convert A String To An Integer

Posted 24 February 2009 - 04:09 PM

I'm no expert, but I think atoi might work. http://cplusplus.com...tdlib/atoi.html

### #3 crazyjugglerdrummer

• GAME OVER. NERD WINS.

Reputation: 121
• Posts: 690
• Joined: 07-January 09

## Re: Simple Way To Convert A String To An Integer

Posted 24 February 2009 - 04:15 PM

You can use atoi(string), which takes a C char array and returns an int. That's the simplest (but not always recommended) way for those.

To do C++ strings, you use a string-stream (or convert to C string and use some other method).
Here's an example from Ubuntuforums.
```#include <iostream>
#include <sstream>

int main()
{
using namespace std;

string s = "1234";
stringstream ss(s); // Could of course also have done ss("1234") directly.

int i;
ss >> i;
cout << i << endl;

return 0;
}
```

stringstreams are used for string IO and processing, and have a bunch of useful stuff in them.

Hope this helps!

### #4 sparkart

Reputation: 114
• Posts: 692
• Joined: 16-February 09

## Re: Simple Way To Convert A String To An Integer

Posted 24 February 2009 - 04:16 PM

http://www.cplusplus...le/isdigit.html

### #5 programble

Reputation: 51
• Posts: 1,315
• Joined: 21-February 09

## Re: Simple Way To Convert A String To An Integer

Posted 24 February 2009 - 05:21 PM

Thanks for your replies, I think I'll use the string stream way.