473,671 Members | 2,580 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 4605
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.********@com acast.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
11826
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
3459
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 good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
16
4876
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 *emptied* after each iteration (and I could not conceive of a practical way doing that). Cheers, Matthias -- Für emails Anweisung in der Adresse befolgen
13
6733
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
11783
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 Dictionary as IDictionary) If Dictionary is Nothing Then Return Dim o as Object = DirectCast(Dictionary, Object)
18
3628
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 - 01,85,78,15 etc... Is there some sort of easy one liner that does this? Hard to search the help files for anything that would assist me in doing this. Thanks in advance.
5
12929
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 password=CAST(@password as binary) However when I go to sql server and convert that binary field to varchar, all I get is the first letter of the password that I typed in. How do I
21
55612
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
3532
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 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical difference?
13
2722
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
8485
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8403
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.