Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Django unicode转换为C++;字符串 我在DJANGO中开发了一个带有WWW接口的C++应用程序。 到目前为止,我在C++中用Booj.Python包装器在Linux中编译为共享对象的工作框架。_C++_Python_Django_Boost - Fatal编程技术网

如何将Django unicode转换为C++;字符串 我在DJANGO中开发了一个带有WWW接口的C++应用程序。 到目前为止,我在C++中用Booj.Python包装器在Linux中编译为共享对象的工作框架。

如何将Django unicode转换为C++;字符串 我在DJANGO中开发了一个带有WWW接口的C++应用程序。 到目前为止,我在C++中用Booj.Python包装器在Linux中编译为共享对象的工作框架。,c++,python,django,boost,C++,Python,Django,Boost,现在我尝试在Django中运行这个框架。当我从表单“CharField”传递字符串时,我得到以下错误: Python argument types in CoreSystem.setOutput(CoreSystem, unicode) did not match C++ signature: setOutput(CoreSystem {lvalue}, std::string) 对此负责的代码如下: form = AnalyzeForm(request.POST) if form.is_va

现在我尝试在Django中运行这个框架。当我从表单“CharField”传递字符串时,我得到以下错误:

Python argument types in
CoreSystem.setOutput(CoreSystem, unicode)
did not match C++ signature:
setOutput(CoreSystem {lvalue}, std::string)
对此负责的代码如下:

form = AnalyzeForm(request.POST)
if form.is_valid():
    cd = form.cleaned_data
    s.setOutput(cd["output"])
其中s是这个CoreSystem对象。如果我这样键入:

s.setOutput("DatabaseOutput")
它很好用。我也使用了str(cd[“output”]),但之后什么也没有发生


我使用django 1.4.1和python 2.7.3

< p>可以使用<代码>编码> <代码>方法将Unicode字符串转换为字节字符串,然后将其发送到期望字符串的C++代码:

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

UTF-8
编码是Unicode字符串的合理默认值。如果
cd[“output”]
已经是ASCII字符串,则编码不会更改它;如果它包含二进制数据,您将得到一个异常。

不幸的是,当我这样做时,什么都没有发生,但没有错误。我甚至在setOutput中放入printf,但它不打印任何内容。就好像根本没有调用setOutput一样。你的答案成功了。上一个错误是由于服务器配置错误。另一个有趣的尝试是重载函数以接受
std::wstring
。如果Boost.Python足够聪明,可以生成自动将
str
转换为
std::string
的包装器,将代码< > Unicode < /Code >转换为 STD::WString 。但是,如何将WString转换为String?@ RoDRigSalasar,在这种情况下,需要使用C++工具实现从宽字符到多字节的编码。对的公认答案显示了一种应可移植的方法。对于同一个问题,C++11提供了一个更简单的解决方案。