Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting int to string but ...

2 views
Skip to first unread message

A Q

unread,
Nov 3, 2001, 12:04:28 PM11/3/01
to
Here is how I convert int to string:

int integerToConvert;
char foo[4];
string ConvertIntoThis;

sprintf(foo, "%d", integerToConvert)
ConvertIntoThis = foo;

And it works! But, what if I don't know how big the integer is. The code
above will only work for integers of length 4. What is a way around this?


Pete Becker

unread,
Nov 3, 2001, 12:17:23 PM11/3/01
to

All you have to do is figure out the length of the longest string that
will result from the conversion. For integral types, look at the minimum
and maximum values that that type can hold.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

John Burton

unread,
Nov 3, 2001, 12:54:33 PM11/3/01
to
"A Q" <aik...@hotmail.com> wrote in message
news:wsVE7.127973$YL3.37...@news3.rdc1.on.home.com...

Use the standard c++ string and ostringstream classes :-

std::ostringstream str;
str << integerToConvert;
std::string foo = str.str();


Ioannis Vranos

unread,
Nov 3, 2001, 1:00:55 PM11/3/01
to
"A Q" <aik...@hotmail.com> wrote in message
news:wsVE7.127973$YL3.37...@news3.rdc1.on.home.com...


Something possibly better:

#include <iostream>
#include <cstdio>

std::string i2s(const int number)
{
static char temp[1024];

std::sprintf(temp, "%d", number);

return std::string(temp);
}

int main()
{
using namespace std;

string number=i2s(121);

cout<<number<<endl;
}


Ioannis
--
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys

Jon Bell

unread,
Nov 3, 2001, 12:58:23 PM11/3/01
to
In article <wsVE7.127973$YL3.37...@news3.rdc1.on.home.com>,

If you're interested in a completely different approach, try using a
std::ostringstream to convert the integer into a std::string:

#include <string>
#include <sstream>

int integerToConvert;
std::string ConvertIntoTnis;
std::ostringstream ConversionStream;

ConversionStream << integerToConvert; // just like writing to cout

ConvertIntoThis = ConversionStream.str(); // extract the resulting string

The std::string automatically expands to accommodate however many
characters are necessary.

--
Jon Bell <jtb...@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

John Burton

unread,
Nov 3, 2001, 1:49:07 PM11/3/01
to
"Jon Bell" <jtb...@presby.edu> wrote in message
news:GM8KL...@presby.edu...

> If you're interested in a completely different approach, try using a
> std::ostringstream to convert the integer into a std::string:
>
> #include <string>
> #include <sstream>
>
> int integerToConvert;
> std::string ConvertIntoTnis;
> std::ostringstream ConversionStream;
>
> ConversionStream << integerToConvert; // just like writing to cout
>
> ConvertIntoThis = ConversionStream.str(); // extract the resulting string

I often add a function like this to my projects, it makes life a lot easier
when I want to output quick debugging string and similar.

template <class T> string ToString(const T& var)
{
ostringstream str;
str << var;
return str.str();
}


patrick

unread,
Nov 3, 2001, 9:48:15 PM11/3/01
to
In article <wsVE7.127973$YL3.37...@news3.rdc1.on.home.com>,
A Q <aik...@hotmail.com> wrote:

Your array is too small. Try making it the maximum size that an integer
can possibly be. 8 digits? Somewhere around there.

Why not cross-post these postsings to ut.cdf.csc228h? I'm sure the
other ppl taking the course are asking the same questions.

-Patrick


>
>
>
>


--
pat...@tendim.cjb.net
http://www.tendim.cjb.net/
Like anime? Got hotline?
hotline://twoaday.cjb.net/

David Harmon

unread,
Nov 3, 2001, 11:01:40 PM11/3/01
to
On Sat, 03 Nov 2001 12:17:23 -0500 in comp.lang.c++,
Pete Becker <peteb...@acm.org> wrote:

>All you have to do is figure out the length of the longest string that
>will result from the conversion. For integral types, look at the minimum
>and maximum values that that type can hold.

See std::numeric_limits<T>::digits10

David Harmon

unread,
Nov 3, 2001, 11:01:43 PM11/3/01
to
On Sat, 3 Nov 2001 20:00:55 +0200 in comp.lang.c++,
"Ioannis Vranos" <noicys@no_.yahoo.s_pam.com> wrote:

> static char temp[1024];

Waste.

0 new messages