日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python调用C的方法

發布時間:2023/12/20 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python调用C的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考:http://www.cnblogs.com/fxjwind/archive/2011/07/05/2098636.html

???????? http://amazingjxq.com/2012/01/09/python%E8%B0%83%E7%94%A8c%E5%87%BD%E6%95%B0/

?

?>>>cd /home/jxq/code/

gcc -fPIC -shared bob_hash.c -o bob_hash.so

然后在python里面用ctypes加載剛才的動態庫。argtypes和restype分別對應函數的參數和返回值的類型。這樣就可以直接調用了。


>>> from ctypes import CDLL, c_int, c_void_p
>>> bob_hash = CDLL('/home/jxq/code/bob_hash.so')
>>> hash_string = bob_hash.hash_string
>>> hash_string.argtypes = [c_void_p]
>>> hash_string.restype = c_int
>>> hash_string('123')

?

?

代碼:

#include <stdio.h>
#include <string.h>
#define mix(a,b,c) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? a -= b; a -= c; a ^= (c >> 13); ? ? ? ?
? ? ? ? b -= c; b -= a; b ^= (a << 8); ? ? ? ? ?
? ? ? ? c -= a; c -= b; c ^= (b >> 13); ? ? ? ?
? ? ? ? a -= b; a -= c; a ^= (c >> 12); ? ? ? ?
? ? ? ? b -= c; b -= a; b ^= (a << 16); ? ? ? ?
? ? ? ? c -= a; c -= b; c ^= (b >> 5); ? ? ? ? ?
? ? ? ? a -= b; a -= c; a ^= (c >> 3); ? ? ? ? ?
? ? ? ? b -= c; b -= a; b ^= (a << 10); ? ? ? ?
? ? ? ? c -= a; c -= b; c ^= (b >> 15); ? ? ? ?
? ? }

unsigned int bob_hash(void *val, unsigned int length)
{
? ? char *k = (char *)val;
? ? unsigned long a,b,c,len;

? ? /* Set up the internal state */
? ? len = length;
? ? a = b = c = 0x9e3779b9; ?/* the golden ratio; an arbitrary value */

? ? /* Handle most of the key */
? ? while (len >= 12)
? ? {
? ? ? ? a += (k[0] +((unsigned long)k[1] << 8) +((unsigned long)k[2] << 16) +((unsigned long)k[3] << 24));
? ? ? ? b += (k[4] +((unsigned long)k[5] << 8) +((unsigned long)k[6] << 16) +((unsigned long)k[7] << 24));
? ? ? ? c += (k[8] +((unsigned long)k[9] << 8) +((unsigned long)k[10]<< 16)+((unsigned long)k[11] << 24));
? ? ? ? mix(a,b,c);
? ? ? ? k += 12; len -= 12;
? ? }

? ? /* Handle the last 11 bytes */
? ? c += length;
? ? switch(len) ? ? ? ? ? ? ?/* all the case statements fall through */
? ? {
? ? case 11: c+=((unsigned long)k[10] << 24);
? ? case 10: c+=((unsigned long)k[9] ?<< 16);
? ? case 9 : c+=((unsigned long)k[8] ?<< 8);
? ? ? ? /* the first byte of c is reserved for the length */
? ? case 8 : b+=((unsigned long)k[7] << 24);
? ? case 7 : b+=((unsigned long)k[6] << 16);
? ? case 6 : b+=((unsigned long)k[5] << 8);
? ? case 5 : b+=k[4];
? ? case 4 : a+=((unsigned long)k[3] << 24);
? ? case 3 : a+=((unsigned long)k[2] << 16);
? ? case 2 : a+=((unsigned long)k[1] << 8);
? ? case 1 : a+=k[0];
? ? }
? ? mix(a,b,c);

? ? return c;
}

int hash_string(void *tmpstr)
{
? ? int hash = (int)bob_hash(tmpstr, strlen(tmpstr));
? ? if(hash < 0)
? ? {
? ? ? ? hash = -hash;
? ? }
? ? return (int)(hash % 200);
}

轉載于:https://www.cnblogs.com/li-baibo/p/3178548.html

總結

以上是生活随笔為你收集整理的Python调用C的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。