Numbers in string declaration

G

gccntn

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.
 
M

mlimber

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-lite/misc-technical-issues.html#faq-39.1

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

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

Cheers! --M
 
M

Marcus Kwok

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-lite/misc-technical-issues.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top