Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) WStrToStr

 

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());
}

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

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

 

 

 

 

 

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);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict