WStrToStr is a convert code snippet to convert a Wt::WString to std::string.
///WStrToStr converts a Wt::WString to std::string //From http://www.richelbilderbeek.nl/CppWStrToStr.htm const std::string WStrToStr(const Wt::WString& s) { return std::string(s.toUTF8()); }
Operating system(s) or programming environment(s)
- Ubuntu 10.10 (maverick)
- Qt Creator 2.0.1
- G++ 4.4.5
Libraries used:
Qt project file: CppWString.pro
#------------------------------------------------- # # Project created by QtCreator 2011-04-16T10:24:05 # #------------------------------------------------- QT += core QT -= gui LIBS += -lwt TARGET = CppWString CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp
#include <cassert> #include <string> #include <Wt/WString> ///StrToWStr converts a std::string to Wt::WString //From http://www.richelbilderbeek.nl/CppStrToWStr.htm const Wt::WString StrToWStr(const std::string& s) { return Wt::WString(s); } ///WStrToStr converts a Wt::WString to std::string //From http://www.richelbilderbeek.nl/CppWStrToStr.htm const std::string WStrToStr(const Wt::WString& s) { return std::string(s.toUTF8()); } int main() { const Wt::WString s = "Hello World"; const std::string t = WStrToStr(s); const Wt::WString u = StrToWStr(t); const std::string v = WStrToStr(u); assert(s == u); assert(t == v); }