473,512 Members | 15,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string type casting to int

I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have is
a string. I've tried searching for a method to convert a string to an
integer, but haven't found a clear answer and was hoping someone here
can help.

Thanks,
- Eli

Jul 25 '06 #1
6 4596
Eli Luong wrote:
I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have
is a string. I've tried searching for a method to convert a string to
an integer, but haven't found a clear answer and was hoping someone
here can help.
Use 'strtol' instead of 'atoi' and read up on 'c_str' member of the
std::string class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '06 #2

Eli Luong ha escrito:
I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have is
a string. I've tried searching for a method to convert a string to an
integer, but haven't found a clear answer and was hoping someone here
can help.

Thanks,
- Eli
I had to do something similar once
You can try to make a new function which creates a an array of char's
big enough to fit in anything you want and read the string element by
element copying it in your array. Once it is done you can use atoi.
First you must find the length of the string.

Note that im a newbie and there is probably an easier and more
effective way

Jul 25 '06 #3
The answer came before i finished my reply

Jul 25 '06 #4
Victor Bazarov <v.********@comacast.netwrote:
Eli Luong wrote:
>I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have
is a string. I've tried searching for a method to convert a string to
an integer, but haven't found a clear answer and was hoping someone
here can help.

Use 'strtol' instead of 'atoi' and read up on 'c_str' member of the
std::string class.
There is also the method using stringstreams given in the FAQ.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 25 '06 #5
Use 'strtol' instead of 'atoi' and read up on 'c_str' member of the
std::string class.

There is also the method using stringstreams given in the FAQ.

Thanks, I'll take a look at both of considerations.

Jul 25 '06 #6
Eli Luong wrote:
I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have is
a string. I've tried searching for a method to convert a string to an
integer, but haven't found a clear answer and was hoping someone here
can help.

Thanks,
- Eli
You may try boost::lexical_cast like:

int aVal = boost::lexical_cast<int>( "1234" );

http://www.boost.org/libs/conversion...t.htm#examples
Jul 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
11819
by: Venkat | last post by:
Hi All, I want to typecast int to std::string how can i do it. Here is the sample code. int NewList; //Fill the NewList with integers values. .......
4
3443
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
4860
by: Der Andere | last post by:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be...
13
6709
by: Matthew Jakeman | last post by:
If i have a pointer, char *. That i have checked is only 1 character long, is just casting it to a char using (char) the best way or is there another way ?
10
11757
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
18
3605
by: Kyro | last post by:
New to C# (migrating from Delphi) and I'm not sure how to get a delimited string into an array of string. input: string looks like - 01-85-78-15-Q11 output: an array with elements - ...
5
12910
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I am having a problem converting string to binary and I am hoping someone can help me out I have a sql query that does an update that updates a binary field calles password ...
21
55574
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
34
3488
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
13
2706
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.