如何在Python中传递函数中的字符串,该功能使用库CTYPES从CPP导出

发布于 2025-02-10 13:44:19 字数 1655 浏览 2 评论 0原文

我正在导出函数加密的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文