There is no method std::to_u16string(...). Obviously static_cast doesn't seem the most appropiate way to make such conversion.
For the opposite conversion, from string to int, a converter may be defined using the function std::stoi(), but from int to u16string it's not working.
I tried the following:
int i = 1234;
std::u16string s;
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
s = convert.from_bytes(std::to_string(i));
std::cout << s.str() << " (" << s.length() << ")" << std::endl;
I also tried to do this:
typedef std::basic_stringstream<char16_t> u16ss;
u16ss ss;
ss << 1234;
std::u16string s = ss.str();
but it doesn't work.
Is there a way to carry out this conversion directly, or there must be some intermediate conversions?
You could try the following:
std::u16string to_u16string(int const &i) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::little_endian>, char16_t> conv;
return conv.from_bytes(std::to_string(i));
}
Live Demo
Related
In my code I want to have a function which performs some calculations based on boost::multiprecision::cpp_dec_float_100 and returns string as a result with some precision, but the question is how I should set the precision ? e.g. If the precision is 9, then I expect the following results for the following numbers:
for 2345.12345678910111213 I expect string "2345.123456789" but I have 12345.1235
for 1.12345678910111213 I expect string "1.123456789" but I have 1.12345679
So it always returns my exact number of characters provided to str() function with round. I know that I can do it like e.g. this:
std::stringstream ss;
ss << std::fixed << std::setprecision(9) << my_value;
return ss.str();
or use cout(but this is not my case) or do some find on string to find "." and take only 9 characters after it, but can I do it in an easier way ? maybe in boost is some function to do it correctly ?
#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
namespace bmp = boost::multiprecision;
std::string foo_1()
{
bmp::cpp_dec_float_100 val{"12345.12345678910111213"};
return val.str(9);
}
std::string foo_2()
{
bmp::cpp_dec_float_100 val{"1.12345678910111213"};
return val.str(9);
}
int main()
{
auto const val_1 = foo_1();
auto const val_2 = foo_2();
std::cout << "val_1: " << val_1 << std::endl;
std::cout << "val_2: " << val_2 << std::endl;
return 0;
}
online version: https://wandbox.org/permlink/HTAHsE5ZE3tgK9kf
Change the line:
return val.str(9);
To:
return val.str(9, std::ios::fixed);
You will get the expected strings.
I was looking for solutions to appending strings with other primitives and found that stringstream was the easiest solution. However, I wanted to streamline the process so I wanted to make a function to ease its use. In case you are proposing alternate methods for concatenation, i need the final result to be char*. I used a loop (i) with:
std::stringstream ss;
ss << "test" << i;
char* name = new char[ss.str().size() + 1];//allocate
strcpy(name, ss.str().c_str());//copy and put (char*)c_str in name
So the output is something link test1test2test3... This was the most reasonable solution I could muster. I was trying to put it into a function for ease of use, but am running into problems. I wanted to do something like:
char* string_to_pointer( char* dest, std::stringstream* _ss ) {
char* result = new char[_ss->str().size() + 1];
strcpy(result, _ss->str().c_str());
return result;
}
I could then do something like:
std::stringstream ss;
ss << "test" << i;
char* name = string_to_pointer( name, &ss );
I'm pretty new to c++ and this seems like the correct use syntactically, but I am running into runtime issues and would welcome solutions on how to get this in an easy to use function without resulting to Boost.
What about something like this:
#include <string>
#include <sstream>
class ToString {
std::ostringstream stream;
public:
template<typename T>
inline ToString &operator<<(const T&val) {
stream << val;
return *this;
}
inline operator std::string() const {
return stream.str();
}
};
You can use it like this:
std::string str = ToString() << "Test " << 5 << " and " << 4.2;
Use the std::stringstream::str() function to retrieve the contents of the string.
Example:
int foo = 42;
double bar = 12.67;
std::stringstream ss;
ss << "foo bar - " << foo << ' ' << bar;
std::string result = ss.str();
If you dont want to modify the string further, you can now simple call result.c_str() to acquire a const char*. However, if you really need a modifyable char* you have to copy the contents of the string to a cstring:
std::unique_ptr<char[]> cstring = new char[result.size() + 1];
strcpy(cstring.get(), result.c_str());
char* string_to_cstring ( const std::string &_ss )
Would be cleaner! Use with string_to_cstring(ss.str())
Need the returned C-String to be changeble? Because if not, just use ss.str().c_str() wherever you need it!
Or use:
char* result = new char[ss.str().size() + 1] (); // value initialized
ss.str().copy(result,std::string::npos);
I know how to do it with cout:
cout << "string" << 'c' << 33;
But how to perform this so output is redirected to variable instead directly to standard out ?
const char* string << "string" << 'c' << 33; //doesn't work
Use std::stringstream from C++ standard library.
It works like the following:
std::stringstream ss;
ss << "string" << 'c' << 33;
std::string str = ss.str();
const char* str_ansi_c = str.c_str();
Keep in mind str still needs to be in the scope while you are using C-style str_ansi_c.
#include <sstream>
#include <iostream>
main()
{
std::stringstream ss;
ss << "string" << 'c' << 33;
std::string str = ss.str();
std::cout << str;
}
How can I convert a double into a const char, and then convert it back into a double?
I'm wanting to convert the double to a string, to write it to a file via fputs, and then when I read the file, it will need to be converted back into a double.
I'm using Visual C++ 2010 Express Edition.
If you just want to write the double values to file, you can simply write it, without converting them into const char*. Converting them into const char* is overkill.
Just use std::ofstream as:
std::ofstream file("output.txt")'
double d = 1.989089;
file << d ; // d goes to the file!
file.close(); //done!
Since you added C++ to your tags, I suggest you use std::stringstream:
#include <sstream>
stringstream ss;
ss << myDouble;
const char* str = ss.str().c_str();
ss >> myOtherDouble;
You can use these functions to convert to and from:
template <class T>
bool convertFromStr(string &str, T *var) {
istringstream ss(str);
return (ss >> *var);
}
template <class T>
string convertToStr(T *var) {
ostringstream ss;
ss << *var;
return ss.str();
}
Example:
double d = 1.234567;
string str = convertToStr<double>(&d);
cout << str << endl;
double d2;
convertFromStr<double>(str, &d2);
cout << d2 << endl;
Use this funcition :
const char* ConvertDoubleToString(double value){
std::stringstream ss ;
ss << value;
const char* str = ss.str().c_str();
return str;
}
i have a string and i need to add a number to it i.e a int. like:
string number1 = ("dfg");
int number2 = 123;
number1 += number2;
this is my code:
name = root_enter; // pull name from another string.
size_t sz;
sz = name.size(); //find the size of the string.
name.resize (sz + 5, account); // add the account number.
cout << name; //test the string.
this works... somewhat but i only get the "*name*88888" and... i don't know why.
i just need a way to add the value of a int to the end of a string
There are no in-built operators that do this. You can write your own function, overload an operator+ for a string and an int. If you use a custom function, try using a stringstream:
string addi2str(string const& instr, int v) {
stringstream s(instr);
s << v;
return s.str();
}
Use a stringstream.
#include <iostream>
#include <sstream>
using namespace std;
int main () {
int a = 30;
stringstream ss(stringstream::in | stringstream::out);
ss << "hello world";
ss << '\n';
ss << a;
cout << ss.str() << '\n';
return 0;
}
You can use string streams:
template<class T>
std::string to_string(const T& t) {
std::ostringstream ss;
ss << t;
return ss.str();
}
// usage:
std::string s("foo");
s.append(to_string(12345));
Alternatively you can use utilities like Boosts lexical_cast():
s.append(boost::lexical_cast<std::string>(12345));
Use a stringstream.
int x = 29;
std::stringstream ss;
ss << "My age is: " << x << std::endl;
std::string str = ss.str();
you can use lexecal_cast from boost, then C itoa and of course stringstream from STL