我试图从我的 Python 代码中调用 C++ 函数,如果我传递一个布尔值或一个int它可以正常工作,但如果我发送一个字符串,它只打印第一个字符。
我正在编译:
g++ -c -fPIC foo.cpp -Wextra -Wall -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so foo.o
python3 fooWrapper.py
Run Code Online (Sandbox Code Playgroud)
这是 C++ 和 Python 代码:
Python:
from ctypes import cdll
lib = cdll.LoadLibrary("./libfoo.so")
lib.Foo_bar("hello")
Run Code Online (Sandbox Code Playgroud)
c++:
#include <iostream>
#include <string>
#include <unistd.h>
void bar(char* string){
printf("%s", string);
}
extern "C" {
void Foo_bar(char* aString){
bar(aString);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道该Boost库,但我无法下载它,并且这种方式除了字符串之外效果很好。感谢您的帮助
我有这个std :: string,其中包含一些跨越多个字节的字符.
当我对此字符串执行子字符串时,输出无效,因为当然,这些字符计为2个字符.在我看来,我应该使用wstring,因为它将这些字符存储为一个元素而不是更多.
所以我决定将字符串复制到wstring中,但是当然这没有意义,因为字符仍然分为2个字符.这只会让情况变得更糟.
将字符串转换为wstring有一个很好的解决方案,将特殊字符合并为1个元素而不是2个元素.
谢谢
我在String^Windows Phone 8 项目的 Cocos2dx 游戏中从我的 C++ WinRT 组件中的 C# 组件回调中得到which包含一些印度语言字符。
每当我将其转换为std::string印地语时,其他字符都会变成垃圾字符。我无法找到为什么会发生这种情况。
这是一个示例代码,我刚刚Platform::String^在此处定义,但考虑它是C++ WinRT Component从 C# 组件传递给的
String^ str = L"?????, ?????, ?????, Vikas";
std::wstring wsstr(str->Data());
std::string res(wsstr.begin(), wsstr.end());
Run Code Online (Sandbox Code Playgroud) 我在 C++ 中有一个 wstring 变量和一个 string 变量。我想将它们连接起来,但简单地将它们添加在一起会产生构建错误。我怎样才能将它们结合起来?如果我需要将 wstring 变量转换为字符串,我将如何完成此操作?
//A WCHAR array is created by obtaining the directory of a folder - This is part of my C++ project
WCHAR path[MAX_PATH + 1] = { 0 };
GetModuleFileNameW(NULL, path, MAX_PATH);
PathCchRemoveFileSpec(path, MAX_PATH);
//The resulting array is converted to a wstring
std::wstring wStr(path);
//An ordinary string is created
std::string str = "Test";
//The variables are put into this equation for concatenation - It produces a build error
std::string result = wStr …Run Code Online (Sandbox Code Playgroud) 我的问题是如何stringstream在wstring或wstringstream
stringstream fileSearch;
fileSearch<<fileOutput.str();
fileSearch<<"*.jpg";
cout<<fileSearch.str()<<endl;
Run Code Online (Sandbox Code Playgroud)
这是我的代码。我想将这个fileSearch字符串流转换成wstring ...有人可以帮我请c ++示例代码...我想在
int numOfFiles(wstring searchPath);
Run Code Online (Sandbox Code Playgroud)
这个功能...
我正在使用casablanca库来序列化json值.
我试图使一个转换的std :: string使用typedef std::wstring string_t和这个从wstring的字符串转换.它编译正常,但程序在执行返回行时崩溃.
std::string getString()
{
web::json::value cvalue;
/* ----- code ----- */
typedef std::wstring string_t;
string_t outputString = cvalue.serialize();
typedef std::codecvt_utf8<wchar_t> convert_type;
std::wstring_convert<convert_type, wchar_t> converter;
std::string converted_str = converter.to_bytes(outputString);
return converted_str;
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么会崩溃.下面是调用该函数的行.
std::string s = getString();
Run Code Online (Sandbox Code Playgroud)
该程序free(_Ptr)在一个名为xdebug的文件中触发了一个断点.我真的不明白这里说的是什么.希望这有助于为您澄清事情.
template<class _Ty>
void __CLRCALL_OR_CDECL _DebugHeapDelete(_Ty *_Ptr)
{ // delete from the debug CRT heap even if operator delete exists
if (_Ptr != 0)
{ // worth deleting
_Ptr->~_Ty();
// delete as _NORMAL_BLOCK, …Run Code Online (Sandbox Code Playgroud)