如何在Python中传递函数中的字符串,该功能使用库CTYPES从CPP导出
我正在导出函数加密
的cpp代码段,该函数在下面的参数,
#include <iostream>
using namespace std;
string Function(char* str)
{
string temp = str;
int length = temp.length();
int disp = length,i=0;
char printChar;
char *store = (char *)malloc(sizeof(char)*100);
while(disp > 0) {
printChar = *(str + length - disp);
if ((printChar >= 65) && (printChar <= 90)) {
if (printChar+disp > 90) {
printChar = ((printChar+disp) % 90) + 64;
store[i] = printChar;
}else{
printChar += disp;
store[i] = printChar;
};
}
else if ((printChar >= 97) && (printChar <= 122)) {
if (printChar+disp > 122) {
printChar = ((printChar+disp) % 122) + 96;
store[i] = printChar;
}else{
printChar += disp;
store[i] = printChar;
};
}
else {
store[i] = printChar;
};
disp -= 1;
i += 1;
};
return store;
}
// The part where i am exporting
extern "C" {
string encrypt(char* str) // this is the functionName(in this case encrypt) which i am giving when exporting
{
return Function(str);
}
}
我应该在我的python代码下传递此导出函数中的字符串有点像:
userString = input()
result = encrypt(userstring) // this is function created in cpp
print(result)
我不知道如何正确使用ctypes。那么请有人可以帮助解决这个问题吗?
cpp code snippet where i am exporting the function encrypt
which takes argument like below
#include <iostream>
using namespace std;
string Function(char* str)
{
string temp = str;
int length = temp.length();
int disp = length,i=0;
char printChar;
char *store = (char *)malloc(sizeof(char)*100);
while(disp > 0) {
printChar = *(str + length - disp);
if ((printChar >= 65) && (printChar <= 90)) {
if (printChar+disp > 90) {
printChar = ((printChar+disp) % 90) + 64;
store[i] = printChar;
}else{
printChar += disp;
store[i] = printChar;
};
}
else if ((printChar >= 97) && (printChar <= 122)) {
if (printChar+disp > 122) {
printChar = ((printChar+disp) % 122) + 96;
store[i] = printChar;
}else{
printChar += disp;
store[i] = printChar;
};
}
else {
store[i] = printChar;
};
disp -= 1;
i += 1;
};
return store;
}
// The part where i am exporting
extern "C" {
string encrypt(char* str) // this is the functionName(in this case encrypt) which i am giving when exporting
{
return Function(str);
}
}
What should be my python code to pass a string in this exported function somewhat like:
userString = input()
result = encrypt(userstring) // this is function created in cpp
print(result)
I don't know how to use ctypes properly. So please can someone help with this question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论