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 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
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
The answer came before i finished my reply
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
.......
|
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...
|
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...
|
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 ?
|
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...
| |
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 - ...
|
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
...
|
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()
|
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...
|
by: Andreas Eibach |
last post by:
Hi,
let's say I have this:
#include <string.h>
#define BLAH "foo"
Later on, I do this:
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |