【发布时间】:2016-09-22 04:53:04
【问题描述】:
我在 C 中创建了一个函数,它接受一个 int 大小和一个 char *buffer 作为参数。我想使用 ctypes 从 python 调用这个函数并传入一个 python byteArray。我知道首先必须将 C 文件编译成共享库(.so 文件)并使用 ctypes 调用该函数。这是我到目前为止的代码。
加密.c:
#include <stdio.h>
void encrypt(int size, unsigned char *buffer);
void decrypt(int size, unsigned char *buffer);
void encrypt(int size, unsigned char *buffer){
for(int i=0; i<size; i++){
unsigned char c = buffer[i];
printf("%c",c);
}
}
void decrypt(int size, unsigned char *buffer){
for(int i=0; i<size; i++){
unsigned char c = buffer[i];
printf("%c",c);
}
}
这是python文件:
import ctypes
encryptPy = ctypes.CDLL('/home/aradhak/Documents/libencrypt.so')
hello = "hello"
byteHello = bytearray(hello)
encryptPy.encrypt(5,byteHello)
encryptPy.decrypt(5,byteHello)
基本上我想从python调用C方法,传递一个python字节数组,让它遍历数组并打印每个元素
【问题讨论】:
标签: python c shared-libraries ctypes