470,855 Members | 1,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Numbers in string declaration

Hello,

I need to embed an integer variable into a string declaration. I tried
the following, but it failed:
int myInt = 16;
string myText = "Value = " + string(myInt);
Can you help me with this?

On another note, I was wondering how I can output the value of an int
in base 16. For instance, how can I write the following in C++?
printf("Value = 0x%x\n",myInt);

Thanks in advance for your help.

May 10 '06 #1
3 1935
gcc...@yahoo.com wrote:
Hello,

I need to embed an integer variable into a string declaration. I tried
the following, but it failed:
int myInt = 16;
string myText = "Value = " + string(myInt);
Can you help me with this?

On another note, I was wondering how I can output the value of an int
in base 16. For instance, how can I write the following in C++?
printf("Value = 0x%x\n",myInt);

Thanks in advance for your help.


For the first question, see:

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

For the second, use <iomanip> and std::hex, like this:

cout << "0x" << hex << myInt;

Cheers! --M

May 10 '06 #2
gc****@yahoo.com wrote:
I need to embed an integer variable into a string declaration. I tried
the following, but it failed:
int myInt = 16;
string myText = "Value = " + string(myInt);
Can you help me with this?
http://www.parashift.com/c++-faq-lit....html#faq-39.1
On another note, I was wondering how I can output the value of an int
in base 16. For instance, how can I write the following in C++?
printf("Value = 0x%x\n",myInt);


std::cout << "Value = 0x" << std::hex << myInt << '\n';

Look up the iostream manipulators for more information.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 10 '06 #3
Thanks for the quick answers. Both replies were right on target!

May 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by /* frank */ | last post: by
6 posts views Thread by patang | last post: by
60 posts views Thread by rhle.freak | last post: by
4 posts views Thread by Chris Forone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.