Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::string to integer type

Reply
Thread Tools

std::string to integer type

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      05-03-2004
Are the C functions atoi() and strtol() (preferred, I'm aware) the
only way to convert std::string's to integers, or are there other
alternatives I'm not aware of?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      05-03-2004
Christopher Benson-Manica <(E-Mail Removed)> writes:

>Are the C functions atoi() and strtol() (preferred, I'm aware) the
>only way to convert std::string's to integers, or are there other
>alternatives I'm not aware of?

See the FAQ - e.g.
http://www.parashift.com/c++-faq-lit...al-issues.html
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      05-03-2004
Tim Love <(E-Mail Removed)> spoke thus:

> See the FAQ - e.g.
> http://www.parashift.com/c++-faq-lit...al-issues.html


Oops!!! I should have known better - sorry for not reading the holy
writ first And unfortunately, I can't use the C++ solution
either, since stringstreams are hit or miss on my (horribly broken)
system. Oh well.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Karthik
Guest
Posts: n/a
 
      05-03-2004
Christopher Benson-Manica wrote:
> I can't use the C++ solution
> either, since stringstreams are hit or miss on my (horribly broken)
> system. Oh well.


Do you mean to say, this is not in accordance with the standard (
and hence, UB ? ) . I am sorry I didnt understand this.

--
Karthik.
Humans please 'removeme_' for my real email.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      05-03-2004
Karthik <(E-Mail Removed)> spoke thus:

> Do you mean to say, this is not in accordance with the standard (
> and hence, UB ? ) . I am sorry I didnt understand this.


No, it means that for a variety of reasons, stringstreams don't behave
on my system. None of those reasons involve UB - it can be summed up
succinctly by saying "My system sucks".

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Russell Hanneken
Guest
Posts: n/a
 
      05-03-2004

"Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
news:c75n54$lb5$(E-Mail Removed)...
> Tim Love <(E-Mail Removed)> spoke thus:
>
> > See the FAQ - e.g.
> > http://www.parashift.com/c++-faq-lit...al-issues.html

>
> Oops!!! I should have known better - sorry for not reading the holy
> writ first And unfortunately, I can't use the C++ solution
> either, since stringstreams are hit or miss on my (horribly broken)
> system. Oh well.


I have two suggestions:

1. See if you can use lexical_cast from the Boost library:

http://www.boost.org/libs/conversion/lexical_cast.htm

It's an elegant solution, and it looks like the author made an attempt to
work around broken C++ implementations.

2. I've rewritten the code in the FAQ to use the deprecated strstream
classes instead of the stringstream classes. Maybe it will work for you:

#ifndef CONVERT_H
#define CONVERT_H

// File: convert.h

// Based on code from the C++ FAQ Lite:
// http://www.parashift.com/c++-faq-lit....html#faq-38.3
// Modified to use strstream classes instead of stringstream classes.

#include <istream>
#include <ostream>
#include <stdexcept>
#include <string>
#include <strstream>
#include <typeinfo>

class BadConversion : public std::runtime_error {
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};

template<typename T>
inline std::string stringify(const T& x)
{
std:strstream o;
if (!(o << x << std::ends))
throw BadConversion(std::string("stringify(")
+ typeid(x).name() + ")");
std::string s(o.str());
o.freeze(false);
return s;
}

template<typename T>
inline void convert(const std::string& s, T& x,
bool failIfLeftoverChars = true)
{
std::istrstream i(s.c_str());
char c;
if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
throw BadConversion(s);
}

template<typename T>
inline T convertTo(const std::string& s,
bool failIfLeftoverChars = true)
{
T x;
convert(s, x, failIfLeftoverChars);
return x;
}

#endif

--
Russell Hanneken
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove the 'g' from my address to send me mail.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Target type ieee.std_logic_1164.std_ulogic in signal assignment isdifferent frim expression type std.standard.integer. Diego UTN-FRP VHDL 4 11-29-2009 01:29 AM
enum promote to bool type rather than Integer type? FE C++ 6 08-04-2009 03:21 PM
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments