How to convert in Django unicode to C++ std::string

I develop an application in C++ with WWW interface in Django. So far I have working framework in C++ with Boost.Python wrapper compiled to shared object in Linux.

Now I try to run this framework in Django. When I pass string from form "CharField" I get this error:

Python argument types in
CoreSystem.setOutput(CoreSystem, unicode)
did not match C++ signature:
setOutput(CoreSystem {lvalue}, std::string)

Code responsible for that is here:

form = AnalyzeForm(request.POST)
if form.is_valid():
    cd = form.cleaned_data
    s.setOutput(cd["output"])

where s is this CoreSystem object. If I type it like this:

s.setOutput("DatabaseOutput")

it works fine. I used also str(cd["output"]) but after that nothing happens.

I'm using Django 1.4.1 and Python 2.7.3

Answers


You can use the encode method to convert a Unicode string to a byte string before sending it off to the C++ code that expects a string:

s.setOutput(cd["output"].encode("utf-8"))

The UTF-8 encoding is a reasonable default for Unicode strings. If cd["output"] is already an ASCII string, encoding will not change it; if it contains binary data, you will get an exception.


Need Your Help

Is it possible to use OutputCache on mvc JsonResult controller methods?

asp.net-mvc json outputcache

I am trying to do this, and it leads to result with contentlen 0

@Html tags are not displayed in a div?

asp.net-mvc-4 html.actionlink

I'm working on asp.Net MVC4 project, and I need to create ActionLink for each element of a list.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.