This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
I want to convert an int variable to string to put it in the textbox.
I have tried this code:
int x = 123;
std::ostringstream osstream;
osstream << x;
std::string string_x = osstream.str();
But it doesn't work.
Try using stringstream instead
std::stringstream osstream;
osstream << x;
std::string string_x = osstream.str();
std::cout << string_x << std::endl;
Works on my computer.
I had another issue once where I had to append a whitespace to that, so try this too:
std::stringstream osstream;
osstream << x << " ";
Related
This question already has answers here:
How to output a character as an integer through cout?
(6 answers)
Convert an int to ASCII character
(11 answers)
Closed 3 years ago.
I have a program that takes a string. Using the check() function that calculate the sum of all the value in the string of integers, I make additional computations, aka ss. The issue comes when I try to convert ss, which is an int, into a char, c.
When I try to print out the newly converted value, nothing prints out on the console, not even an error message.
I have tried using static_cast<char>(ss), and it won't work. Yet when I try to print out the ss value, I get it to print it out.
Source Code
void sum(string input)
{
int s = check(input);
int ss = (s * 9) % 10;
char c = ss;
cout << "val is: " << c << endl;
}
int main()
{
string x = "7992739871";
sum(x);
return 0;
}
Can someone explain what I might be doing wrong and how it can be fixed?
You can use std::to_string() (C++11) but making sure that the value of c is something that can be printable is a better practice.
This question already has answers here:
Integer to hex string in C++
(27 answers)
Closed 5 years ago.
I have an int which I want to convert to a char array, but I want the char array to be formatted in hexadecimal and with every byte of the int taking up exactly 2 char variables (filled out with zeroes).
To clarify what I mean, I have an example:
I want the int 232198 (0x38b06) to become "00038b06".
I can of course acomplish this by using this code:
#include <iostream>
#include <iomanip>
int main()
{
std::cout <<
std::hex <<
std::setw(8) <<
std::setfill('0') <<
232198 <<
std::endl;
return 0;
}
Which prints out:
00038b06
But that only prints it out to the console, and as I mentioned before, want to store it a char array.
I don't care if the code is portable or not, this just has to work for windows.
stringstreams are useful to do this:
std::stringstream sstr;
sstr <<
std::hex <<
std::setw(8) <<
std::setfill('0') <<
232198;
std::string str = sstr.str();
now str contains the formatted number. str.c_str() will give you a const char*.
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
For example, i have the following string :
std::string s = "Hello, World!"
I want the address of the last element of s which is '!'.
I tried the following code, but it does not seem to output what I want it to.
std::cout << &s[s.length() - 1];
That outputs '!' not it's address, the same happens with s.back()
Is it because of the formatting caused by std::cout or is the problem elsewhere?
Basically I want a function that outputs the address of the last (and if possible, the first) element in a string.
Currently, you're using the overload of operator<< that takes a const char* as input. It treats the input as a null-terminated C string.
If you cast to (const void*) the problem will go away:
std::cout << (const void*)(&s[s.length() - 1]);
auto address_back = &s.back();
auto address_front = &s.front();
cout << static_cast<void*>(address_back) << endl;
cout << static_cast<void*>(address_front) << endl;
This question already has answers here:
How can I pad an int with leading zeros when using cout << operator? [duplicate]
(7 answers)
Closed 9 years ago.
Here's the code I'm trying to change
string binary = "000000100001000100010000000100000"
bitset<32> set(binary);
cout << hex << set.to_ulong() << endl;
The code shows 2112010 but I want it to show 02112010.
std::cout << std::setfill('0') << std::setw(5) << i << std::endl;
that is the same number you can format it with the 0 by using format specifiers if you need to retain the zero you need to store it as a string,
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ convert hex string to signed integer
I have the string line which is a hexadecimal number say like 12ab43c..(but I have read it as a string) and I would like to pass it to an unsigned char* linehex or directly to a hexadecimal so I can later use it in my program for further computations.
Which is the most efficient way to do this?
The easiest is probably to read it as a number to start with, instead of reading it as a string, then converting. For example:
some_stream >> std::hex >> your_number;
Quick demo code:
#include <iostream>
int main() {
int x;
std::cin >> std::hex >> x;
std::cout << x << "\n";
return 0;
}
Input: ff
Output: 255