Connecting Tech Pros Worldwide Help | Site Map

int to char

wak
Guest
 
Posts: n/a
#1: Jul 22 '05
hi!
how can I convert an int to char !?

string foo = "Hello";
for (int i=0 ; i <10 ; i++)
cout << foo.append(i);




--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Peter van Merkerk
Guest
 
Posts: n/a
#2: Jul 22 '05

re: int to char


> hi![color=blue]
> how can I convert an int to char !?[/color]

I guess you intended to ask "how can I convert an int to a string"
[color=blue]
> string foo = "Hello";
> for (int i=0 ; i <10 ; i++)
> cout << foo.append(i);[/color]

http://www.parashift.com/c++-faq-lit....html#faq-38.1

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Christoph Rabel
Guest
 
Posts: n/a
#3: Jul 22 '05

re: int to char


wak wrote:[color=blue]
> hi!
> how can I convert an int to char !?[/color]

You can use a stringstream or sprintf.
[color=blue]
> string foo = "Hello";
> for (int i=0 ; i <10 ; i++)
> cout << foo.append(i);[/color]

cout << foo << i;

should work in this case. (It doesnt the same what you have written but
probably what you want)

In case you really want the above behaviour, you could, in this example
do it the following way:

for (int i=0 ; i <10 ; i++)
{
foo += '0' + i;
cout << foo;
}

Please be advised, that it only works with ints in the range 0-9 and
that C++98 does not guarantee that the letters 0-9 are in this order.

But C guarantees it and the C++2003 standard. (Please correct me if Im
wrong in this) So it should be fairly safe to use.

hth

Christoph
Jeremy Cowles
Guest
 
Posts: n/a
#4: Jul 22 '05

re: int to char



"Christoph Rabel" <odie@hal9000.vc-graz.ac.at> wrote in message
news:3fd5a06a$0$18044$3b214f66@aconews.univie.ac.a t...[color=blue]
> wak wrote:[color=green]
> > hi!
> > how can I convert an int to char !?[/color]
>
> You can use a stringstream or sprintf.[/color]

So C++ has no simple way to concat a string and other variables? For
example, in other languages (using C++ syntax):

//--

String s("my string is");
String n("long.");
int i = 99;

s += i + n;
s += i.ToString( ) + n;
s += ((string) i ) + n;

//--

You can't do that in C++? I am reading a C++ book, and I was wondering why
the author never described how to concat vars (aside from the insertion
operator)... That seems weird.

TIA,
Jeremy

Chris \( Val \)
Guest
 
Posts: n/a
#5: Jul 22 '05

re: int to char



"wak" <wak@libero.it> wrote in message
news:63d5a65faa0876a048aead8a1c90d7c3.71413@mygate .mailgate.org...
| hi!
| how can I convert an int to char !?
|
| string foo = "Hello";
| for (int i=0 ; i <10 ; i++)
| cout << foo.append(i);

static_cast<char>( i )

Cheers.
Chris Val


Karl Heinz Buchegger
Guest
 
Posts: n/a
#6: Jul 22 '05

re: int to char


Jeremy Cowles wrote:[color=blue]
>
> "Christoph Rabel" <odie@hal9000.vc-graz.ac.at> wrote in message
> news:3fd5a06a$0$18044$3b214f66@aconews.univie.ac.a t...[color=green]
> > wak wrote:[color=darkred]
> > > hi!
> > > how can I convert an int to char !?[/color]
> >
> > You can use a stringstream or sprintf.[/color]
>
> So C++ has no simple way to concat a string and other variables? For
> example, in other languages (using C++ syntax):
>
> //--
>
> String s("my string is");
> String n("long.");
> int i = 99;
>
> s += i + n;
> s += i.ToString( ) + n;
> s += ((string) i ) + n;
>
> //--
>
> You can't do that in C++?[/color]

You can do everything in C++.
It's just that some things don't come 'out of the box'.
For some things you need to do a little bit of work by yourself.
But once you have done that you have that functionality:

Taken from the FAQ and modified a little bit

inline std::string stringify(int x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

can be used as in:

std::string s( "my string is" );
std::string n( "long." );
int i = 99;

s += stringify( i ) + n;

You might also go to http://www.boost.org and look up lexical_cast.
Basically it does the very same: convert whatever you give to it into
a string. Whatever you do with that string is then up to you.
[color=blue]
> I am reading a C++ book, and I was wondering why
> the author never described how to concat vars (aside from the insertion
> operator)... That seems weird.[/color]

It's very simple: First convert everything to a std::string, then concat
all the individual pieces.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
jeffc
Guest
 
Posts: n/a
#7: Jul 22 '05

re: int to char



"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:5ikBb.34201$b01.831950@twister.tampabay.rr.co m...[color=blue]
>
> "Christoph Rabel" <odie@hal9000.vc-graz.ac.at> wrote in message
> news:3fd5a06a$0$18044$3b214f66@aconews.univie.ac.a t...[color=green]
> > wak wrote:[color=darkred]
> > > hi!
> > > how can I convert an int to char !?[/color]
> >
> > You can use a stringstream or sprintf.[/color]
>
> So C++ has no simple way to concat a string and other variables? For
> example, in other languages (using C++ syntax):
>
> //--
>
> String s("my string is");
> String n("long.");
> int i = 99;
>
> s += i + n;
> s += i.ToString( ) + n;
> s += ((string) i ) + n;
>
> //--
>
> You can't do that in C++?[/color]

You can't do that in C++ because you're using "int", which is a built in
type from the C days. It's not a class. You could easily write one of
course, but your code wouldn't be portable.


jeffc
Guest
 
Posts: n/a
#8: Jul 22 '05

re: int to char



"Chris ( Val )" <chrisval@bigpond.com.au> wrote in message
news:br4jju$2871kd$1@ID-110726.news.uni-berlin.de...[color=blue]
>
> "wak" <wak@libero.it> wrote in message
> news:63d5a65faa0876a048aead8a1c90d7c3.71413@mygate .mailgate.org...
> | hi!
> | how can I convert an int to char !?
> |
> | string foo = "Hello";
> | for (int i=0 ; i <10 ; i++)
> | cout << foo.append(i);
>
> static_cast<char>( i )[/color]

That wasn't very nice.


Closed Thread


Similar C / C++ bytes