This question already has answers here:
Append an int to a std::string [duplicate]
(4 answers)
Closed 10 years ago.
How can I convert from unsigned short to string using C++?
I have tow unsigned short variables:
unsigned short major = 8, minor = 1;
I want to join them for on string, looks like:
std::string version = major + "." + minor;
how can I do it? will aprrechiate a small sample code.
Thanks
could use std::stringstream or std::to_string(C++11) or boost::lexical_cast
#include<sstream>
std::stringstream ss;
ss << major << "." << minor;
std::string s = ss.str();
std::to_string:
std::string s = std::to_string(major) + "." +std::to_string(minor);
In C++11, you don't need some stream do do this:
std::string version = std::to_string(major)
+ "." + std::to_string(minor);
std::ostringstream oss;
oss << major << "." << minor;
Receive the generated string via oss.str().
Use std::ostringstream. You need to include the header <sstream>.
std::ostringstream ss;
ss << major << "." << minor;
std::cout << ss.str();
Related
This question already has answers here:
Integer to hex string in C++
(27 answers)
Closed 7 years ago.
I'm new to C++ and I have a simple and stupid issue and I hope someone can help me!
I have a byte, for example:
uint8_t MyByte = 0x0C;
and I want to convert it into a string corresponding to the hexadecimal MyByte value, in this case "0C".
If I try:
std::string MyString = std::to_string(MyByte);
I will obtain: MyString = "12"; I want to obtain MyString = "0C" instead, that is the corresponding hex value.
Is it possible?
Thanks!
EDIT:
I know there is a similar question on the link provided, but it is not right form me. In fact, if I try:
std::stringstream stream;
stream << std::hex << MyByte;
std::string MyString( stream.str() );
MyString doesn't shows me as expected.
I just tryied this solution, it seems it's working:
std::stringstream stream;
stream << std::hex << (int)MyByte; // cast needed
std::string MyString( stream.str() );
std::transform(strBcc.begin(), strBcc.end(),strBcc.begin(), ::toupper);
You can use std::ostringstream and std::hex:
std::ostringstream oss;
oss << std::hex << (int)MyByte;
To obtain the string use
std::string MyString = oss.str();
This question already has answers here:
Easiest way to convert int to string in C++
(30 answers)
Closed 8 years ago.
Here str2 is a string I need to append and str1 is the string I append onto str2. After I append last to str2 I need to append a number (int cnt) to str2. So I am using the below code, which came to my mind and it is working. Is it wrong to code like this, since I saw the usage of string s = lexical_cast<string>(a); and itoa (i,buffer,10); implementations where compiler complaints about the library.
string str2;
string str1;
int cnt;
str2 += str1 ;
str2 += char(cnt+48);//cnt converted to ASCII char and appended;
This statement
str2 += char(cnt+48);
is bad. Firstly it uses magic number 48. It would be better to write at least as
str2 += char( cnt + '0' );
Secondly the code will work only if cnt contains a number with one digit.
It would be better to use standard function std::to_string For example
str2 += std::to_string( cnt );
If you don't want to use c++11 and its std::to_string(...) you can use ostringstream class.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
ostringstream ss;
ss << 1;
string str = ss.str();
cout << str << endl;
return 0;
}
Output:
1
I need to create files with generated names. I use boost::lexical_cast to transform integers to std::string. Is it a possibility to get string with padding zeros;
I have no c++11 tools, just everything that MSVS 2008 supports.
Example :
int i = 10;
std::string str = boost::lexical_cast<std::string>(i);
// str = "10"
// expect str = "000010"
p.s. don't suggest to use sprintf please.
Why boost::lexical_cast? Use std::stringstream
std::ostringstream ss;
ss << std::setw(6) << std::setfill('0') << i;
const std::string str = ss.str();
You could use std::ostringstream with the normal stream manipulators for formatting.
This question already has answers here:
How to concatenate a std::string and an int
(25 answers)
Closed 8 years ago.
I'm new to C++ and working on a simple project. Basically where I'm encountering a problem is creating a file with a number (int) in the file name. As I see it,I have to first convert the int to a string (or char array) and then concatenate this new string with the rest of the file name.
Here is my code so far that fails to compile:
int n; //int to include in filename
char buffer [33];
itoa(n, buffer, 10);
string nStr = string(buffer);
ofstream resultsFile;
resultsFile.open(string("File - ") + nStr + string(".txt"));
This gives a couple compilation errors (compiling in Linux):
itoa not declared in this scope
no matching function for call to ‘std::basic_ofstream char, std::char_traits char ::open(std::basic_string char, std::char_traits char , std::allocator char )’
I've tried the advice here: c string and int concatenation
and here: Easiest way to convert int to string in C++ with no luck.
If I using the to_string method, I end up with the error "to_string not a member of std".
You could use a stringstream to construct the filename.
std::ostringstream filename;
filename << "File - " << n << ".txt";
resultsFile.open(filename.str().c_str());
For itoa, you are likely missing #include <stdlib.h>. Note that itoa is non-standard: the standard ways to format an integer as string as sprintf and std::ostringstream.
ofstream.open() takes a const char*, not std::string. Use .c_str() method to obtain the former from the latter.
Putting it together, you are looking for something like this:
ostringstream nameStream;
nameStream << "File - " << n << ".txt";
ofstream resultsFile(nameStream.str().c_str());
You want to use boost::lexical_cast. You also need to include any needed headers:
#include <boost/lexical_cast>
#include <string>
std::string nStr = boost::lexical_cast<std::string>(n);
then it's simply:
std::string file_name = "File-" + nStr + ".txt";
because std::strng plays nicely with string literals (e.g. ".txt").
Using std::ostringstream:
std::ostringstream os;
os << "File - " << nStr << ".txt";
std::ofstream resultsFile(os.str().c_str());
Using std::to_string (C++11):
std::string filename = "File - " + std::to_string(nStr) + ".txt";
std::ofstream resultsFile(filename.c_str());
for itoa function
include <stdlib.h>
consider this link
http://www.cplusplus.com/reference/cstdlib/itoa/
You can use std::stringstream
std::stringstream ss;
ss << "File - " << n << ".txt";
Since the constructor requires a char pointer, you need to convert it into a char pointer using
ofstream resultsFile(ss.str().c_str());
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert double to string C++?
however, thanks everyone, I have a float number:1.000, how to convert it to a std::string ,like "1.000".
std::stringstream stream;
float f = 1.000f;
stream << f;
std::string str;
stream >> str;
You can either use stringstream:
float val = 1.000f;
stringstream ss;
ss << val;
string stringVal = ss.str();
or use boost::lexical_cast<>() (which also uses stringstream underneath)
#include <boost/lexical_cast.hpp>
float val = 1.000f;
string stringVal = boost::lexical_cast<string>( val );