Topic: int to string?
Author: "David Thompson" <david.thompson1@worldnet.att.net>
Date: 2000/11/22 Raw View
Achim Oetringhaus <achim.oetringhaus@gmx.de> wrote :
....
> In C you can use sprintf or itoa. The latter is not an ISO-C
> function but many libraries support it.
>
Correct.
> The drawback of both functions is that you cannot limit the
> number of characters which the function will produce, i.e. you
> might exceed the limit of the char array.
>
It's not hard to compute a reasonably good upper bound on
the number of digits needed, although a minor nuisance.
> Here, snprintf helps, but once again its not ISO. My Linux system
> has it, for example.
>
snprintf (also vsnprintf) is now standard in "C99" (ISO 9899:1999)
although no C99 implementations have been spotted in the wild yet
--
- David.Thompson 1 now at worldnet.att.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Jan van Mansum <jvm@ysabel.com>
Date: 2000/11/15 Raw View
Hello group,
Is there any facility in the standard library that lets you convert an
integer to a string? It is of course possible to implement it, but if it
already exists I prefer to use the standard library.
Thanks and regards,
Jan van Mansum.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Niklas Mellin <nimel@my-deja.com>
Date: 2000/11/15 Raw View
In article <3A0F3F1D.A48541F2@ysabel.com>,
Jan van Mansum <jvm@ysabel.com> wrote:
> Hello group,
>
> Is there any facility in the standard library that lets you convert an
> integer to a string? It is of course possible to implement it, but if
it
> already exists I prefer to use the standard library.
There are several ways. One way to do it is:
int x = 47;
ostringstream ss;
ss << x;
string str = ss.str();
/Niklas Mellin
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: achim.oetringhaus@gmx.de (Achim Oetringhaus)
Date: 2000/11/15 Raw View
In article <3A0F3F1D.A48541F2@ysabel.com>, Jan van Mansum <jvm@ysabel.com> wrote:
> Hello group,
>
> Is there any facility in the standard library that lets you convert an
> integer to a string? It is of course possible to implement it, but if it
> already exists I prefer to use the standard library.
>
Which standard library? C or C++?
In C you can use sprintf or itoa. The latter is not an ISO-C
function but many libraries support it.
The drawback of both functions is that you cannot limit the
number of characters which the function will produce, i.e. you
might exceed the limit of the char array.
Here, snprintf helps, but once again its not ISO. My Linux system
has it, for example.
Achim.
achim.oetringhaus@gmx.de
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "KK Poon" <kkpoon@pacific.net.hk>
Date: 2000/11/15 Raw View
Hello Jan,
There is a function called 'itoa' in 'stdlib.h'.
Regards,
KK Poon
Jan van Mansum <jvm@ysabel.com> wrote in message
news:3A0F3F1D.A48541F2@ysabel.com...
> Hello group,
>
> Is there any facility in the standard library that lets you convert an
> integer to a string? It is of course possible to implement it, but if it
> already exists I prefer to use the standard library.
>
> Thanks and regards,
>
> Jan van Mansum.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
> [ Note that the FAQ URL has changed! Please update your bookmarks. ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Fabian Csaba" <csaba.fabian@sysdata.siemens.hu>
Date: 2000/11/15 Raw View
I guess the plain C sprintf is standard...
But that's only a guess...
Regards...
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: kuehl@ramsen.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 2000/11/15 Raw View
Hi,
Jan van Mansum (jvm@ysabel.com) wrote:
: Is there any facility in the standard library that lets you convert an
: integer to a string? It is of course possible to implement it, but if it
: already exists I prefer to use the standard library.
My preferred approach is the "convert everything to everything" function,
ie. 'stream_cast<>()':
#include <sstream>
template <typename S, typename T>
S stream_cast(T const& t) {
std::stringstream stream;
stream << t;
S rc;
if (!(stream >> rc))
throw std::exception("conversion failed");
return rc;
}
You would use it like this:
std::string number = stream_cast<std::string>(17);
See also Boost's library for an implementation of this stuff.
--
<mailto:dietmar_kuehl@yahoo.de>
<http://www.fmi.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Josh Sebastian <curien@earthlink.net>
Date: 2000/11/15 Raw View
In article <8uubln$l7d$1@hfc.pacific.net.hk>,
"KK Poon" <kkpoon@pacific.net.hk> wrote:
> Hello Jan,
>
> There is a function called 'itoa' in 'stdlib.h'.
No such function exists in either C or C++.
_______
"Yields falsehood when preceded by its quotation" yields falsehood when
preceded by its quotation.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 2000/11/15 Raw View
In article <8uubln$l7d$1@hfc.pacific.net.hk>, KK Poon
<kkpoon@pacific.net.hk> writes
>Hello Jan,
>
>There is a function called 'itoa' in 'stdlib.h'.
Since when??
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: comeau@panix.com (Greg Comeau)
Date: 2000/11/15 Raw View
In article <3A0F3F1D.A48541F2@ysabel.com>,
Jan van Mansum <jvm@ysabel.com> wrote:
>Is there any facility in the standard library that lets you convert an
>integer to a string? It is of course possible to implement it, but if it
>already exists I prefer to use the standard library.
There is no such standard facility as in _a_ specific
routine name dedicated to just doing integers to strings.
However, there are various more general facilities.
Check out http://www.comeaucomputing.com/techtalk/#itoa
- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: comeau@panix.com (Greg Comeau)
Date: 2000/11/16 Raw View
In article <8uubln$l7d$1@hfc.pacific.net.hk>,
KK Poon <kkpoon@pacific.net.hk> wrote:
>There is a function called 'itoa' in 'stdlib.h'.
You may happen to have a stdlib.h which contains an itoa
but all don't. Check out http://www.comeaucomputing.com/techtalk/#noitoa
- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Rudolf" <rudolfrodnasa@rodnasa.se>
Date: 2000/11/16 Raw View
That is Microsoft specific and not part of the language.
"KK Poon" <kkpoon@pacific.net.hk> wrote in message
news:8uubln$l7d$1@hfc.pacific.net.hk...
> Hello Jan,
>
> There is a function called 'itoa' in 'stdlib.h'.
>
> Regards,
> KK Poon
>
> Jan van Mansum <jvm@ysabel.com> wrote in message
> news:3A0F3F1D.A48541F2@ysabel.com...
> > Hello group,
> >
> > Is there any facility in the standard library that lets you convert an
> > integer to a string? It is of course possible to implement it, but if it
> > already exists I prefer to use the standard library.
> >
> > Thanks and regards,
> >
> > Jan van Mansum.
> >
> > ---
> > [ comp.std.c++ is moderated. To submit articles, try just posting
with ]
> > [ your news-reader. If that fails, use
lto:std-c++@ncar.ucar.edu ]
> > [ --- Please see the FAQ before
]
> > [ FAQ:
arch.att.com/~austern/csc/faq.html ]
> > [ Note that the FAQ URL has changed! Please update your
marks. ]
> >
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
> [ Note that the FAQ URL has changed! Please update your bookmarks. ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Bernd Jochims" <bernd@nucleus.com>
Date: Fri, 17 Nov 2000 00:56:31 GMT Raw View
"Jan van Mansum" <jvm@ysabel.com> wrote in message
news:3A0F3F1D.A48541F2@ysabel.com...
> Hello group,
>
> Is there any facility in the standard library that lets you convert an
> integer to a string? It is of course possible to implement it, but if it
> already exists I prefer to use the standard library.
>
> Thanks and regards,
>
> Jan van Mansum.
>
> ---
///
Below is a pre STD way of doing it.
Actually, I preferred the template function in article
news:8uujrk$6t3$1@news.BelWue.DE...
for the way it is performing the conversion.
#include <iostream>
#include <strstream>
#include <string>
using namespace std;
char* int_to_cstring(int a, char* str)
{
strstream ss;
ss<<" Hello, my int is now in a char string showing a value of "<<a<<ends;
strcpy(str,ss.str());
return str;
}
std::string int_to_stdstring(int a)
{
strstream ss;
ss<<" Hello, my int is now copied to a standard string with a value of "
<<a<<ends; // ends or '\0' must be appended to the stream
return string(ss.str());
}
int main()
{
int x=45;
char str[80];
strcpy(str, int_to_cstring(x,str));
cout<<str<<endl;
strcpy(str, int_to_cstring(x+=250000,str));
cout<<str<<endl;
string s =int_to_stdstring(x+20);
cout<<s<<endl;
s =int_to_stdstring(x+55);
cout<<s<<endl;
return 0;
}
--
******************
Bernd Jochims
bernd@nucleus.com
Calgary, Alberta
Canada
******************
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Daniel Frey <d.frey@aixigo.de>
Date: 2000/11/17 Raw View
Hi,
Dietmar Kuehl wrote:
> My preferred approach is the "convert everything to everything" function,
> ie. 'stream_cast<>()':
>
> #include <sstream>
> template <typename S, typename T>
> S stream_cast(T const& t) {
> std::stringstream stream;
> stream << t;
> S rc;
> if (!(stream >> rc))
> throw std::exception("conversion failed");
> return rc;
> }
>
> You would use it like this:
>
> std::string number = stream_cast<std::string>(17);
>
> See also Boost's library for an implementation of this stuff.
Excellent function! Just wondering: Why do you use std::exception?
Wouldn't std::bad_cast be more meaningfull to the user?
Regards, Daniel
BTW: I can't find this function at boost.org... probably (hopefully) it
will be added in the next release...
--
Daniel Frey
aixigo AG - Financial Research and Education
Hammerweg 4, 52074 Aachen, Germany
Tel: +49 (0)241 96106-17, Fax: +49 (0)241 96106-20
EMail: D.Frey@aixigo.de
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: kuehl@ramsen.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 2000/11/17 Raw View
Hi,
Daniel Frey (d.frey@aixigo.de) wrote:
: Excellent function!
I think Kevlin Henney came up with this. I think it goes under the name
"lexical_cast<>()" but I'm not sure.
: Just wondering: Why do you use std::exception?
I simply didn't remember 'bad_cast' when writing the article but I
agree that this would be a better choice. In a real implementation I
think there are other minor details I would change, too (but I havn't
though about the details too hard).
--
<mailto:dietmar_kuehl@yahoo.de>
<http://www.fmi.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: sirwillard@my-deja.com
Date: 2000/11/17 Raw View
In article <3A14F623.586C5CA0@aixigo.de>,
Daniel Frey <d.frey@aixigo.de> wrote:
> BTW: I can't find this function at boost.org... probably (hopefully)
it
> will be added in the next release...
This hasn't been formally submitted to Boost if I'm correct. That or
it's submitted and will soon go through review. In any event, to check
it out before acceptance you'll have to join the Boost eGroup as
detailed in the Membership section on http://www.boost.org. You'll
find it in the Files section on the eGroup at
http://www.egroups.com/files/boost/lexical_cast/. If you find things
like this of interest I'd strongly suggest you join the eGroup mailing
list and would even encourage you to participate.
--
William E. Kempf
Software Engineer, MS Windows Programmer
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]