How i convert from Wt::WString to std::string
Added by Chris immer about 8 years ago
Hello,
Excuse me for my rookie question. I'm new in the C and Wt world :)
I have problems with the Wt::WString and std::string c .
I would like to make a normal C string from a composite WString. Unfortunately, it fails with the following error:
error: cannot convert 'Wt::WString' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' in assignment
dest->valueText() + " " + user->text() + " " + passwort->text() ;
^
clientTab.hpp:64:51: error: no matching function for call to 'Wt::WText::setText(std::__cxx11::string*&)'
target_name_box->setText( target_name_command );
can you help me, please?
Wt::WSelectionBox* dest;
Wt::WText* target_name_box;
std::string* target_name_command;
void update_text_box() {
// output update helper
target_name_command =
"iqn." + date->text() + ".de.xxx." + ip->text() + ":" +
hardware->text() + ":" + button_label + "." + mac->text() + ".img" +
" " + volume->valueText() + " " + image->valueText() + " " +
dest->valueText() + " " + user->text() + " " + passwort->text() ;
target_name_box->setText( target_name_command );
pbCheck->setCommand( key_to_command["controller"] + target_name_command );
Replies (4)
RE: How i convert from Wt::WString to std::string - Added by Wim Dumon about 8 years ago
Hey Chris,
Wt needs some hint regarding the character encoding of the strings you want to convert WString to a std::wstring. Given UTF-8's popularity nowadays, this is usually as simple as calling fromUTF8() and toUTF8() on WStrings:
"iqn." + date->text().toUTF8() + ...
On the other hand, looking at your code, I would suggest you skip the conversion between WString and std::string alltogether in your concatenation step:
Wt::WSelectionBox* dest;
Wt::WText* target_name_box;
// note: std::string* or WString* would not work here, this has to be a value variable
WString target_name_command;
void update_text_box() {
// output update helper
target_name_command =
"iqn." + date->text() + ".de.xxx." + ip->text() + ":" +
hardware->text() + ":" + button_label + "." + mac->text() + ".img" +
" " + volume->valueText() + " " + image->valueText() + " " +
dest->valueText() + " " + user->text() + " " + passwort->text() ;
target_name_box->setText( target_name_command );
pbCheck->setCommand( key_to_command["controller"] + target_name_command );
Then convert your target command to an std::string when you need to, in the proper encoding (for UTF-8, this would be target_name_command.toUTF8()).
Wim.
RE: How i convert from Wt::WString to std::string - Added by Chris immer about 8 years ago
hi Wim,
thank you for your quick reply. It works.
the reason, why i'm try to converting it. I give the string to a bash script and always i get a $'\r' carriage return in the end of my string. I don't want that. Do you have an idea why?
RE: How i convert from Wt::WString to std::string - Added by Wim Dumon about 8 years ago
Chris,
Sorry, I can't see a reason why the \r would be added. This should work:
std::string command_string = target_name_command.toUTF8();
system(command_string.c_str());
Carefully sanitize your inputs, or you have just created an easily exploitable attack vector :)
Wim.
RE: How i convert from Wt::WString to std::string - Added by Chris immer about 8 years ago
thanks for your hint,
you mean my wt inputs are not so good?
is that right? i am grateful about every tip or link.