参考: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的方法相关推荐

  1. python调用java的方法

    最近自己开发的一套测试平台,因为上游系统经常修改主数据,导致其中一个功能经常失败,要频繁找上游测试帮忙修改数据. 基于此种原因,对于这种过于依赖上游系统的接口,决定放弃直接调上游系统的http请求下发 ...

  2. python源程序执行的方式是什么执行-python调用可执行文件的方法

    最近要用到python调用C程序,因此,看了一下python调用别的程序的方法.大致来说,python调用C/C++有两种方式,一种是调用C编译的动态链接库,即so文件,一种是调用C生成的可执行文件. ...

  3. 简单python脚本实例-简单了解python调用其他脚本方法实例

    1.用python调用python脚本 #!/usr/local/bin/python3.7 import time import os count = 0 str = ('python b.py') ...

  4. 用python读取身份证信息的功能分析与实现,兼述python调用dll的方法

    背景 有这样一个需求,要求能自动读取用户的身份证信息.如果是一代身份证,这个功能恐怕只能通过图像识别的办法来解决了.不过现在二代身份证已经很普及.客户要求能读二代身份证就可以了. 现在二代身份证阅读器 ...

  5. python调用java代码方法

    前言: 公司要测试对外接口,接口中的数据又涉及到加密的问题,而python对加密数据进行位运算后,居然无法逆向位运算回来了(应该是个人能力不够吧),只好调用同事之前用的java代码中的函数去解码了,花 ...

  6. Python调用MATLAB的方法(mlab接口库)(未总结)

    一.安装 安装mlab库 pip install mlab 显示错误 No module named 'docutils'  (不知道这是什么库) pip install docutils   (安装 ...

  7. python调用随机分层抽样方法_python实现的分层随机抽样案例

    昨天写了一段用来做分层随机抽样的代码,很粗糙,不过用公司的2万名导购名单试了一下,结果感人,我觉得此刻的我已经要上天了,哈哈哈哈哈哈 代码如下: #分层随机抽样 stratified sampling ...

  8. python调用c++动态库 linux_linux中使用boost.python调用c++动态库的方法

    前言 最近开始使用 robot framework 测试c++的动态库,robot framework 是跑在 windows 上面,c++动态库是跑在远程linux主机上面.测试办法是让 robot ...

  9. python调用c++动态库_Python应用:调用C/C++动态链接库的方法详解

    Linux系统下调用动态库(.so) 1.linuxany.c代码如下: #include "stdio.h" void display(char* msg){ printf(&q ...

最新文章

  1. 为何多线程就能提高Java程序的执行效率
  2. MySQL配置文件参数详解
  3. 2022年美国大学生数学建模竞赛O奖流程图
  4. Realm数据库版本迁移
  5. python中常见的运行时错误_17个常见的Python运行时错误
  6. Odoo (OpenERP/TinyERP)-10.0 (Debian 8)
  7. 2020,再见;2021,我来了!
  8. linux进程闭锁,4. ps 进程查看器
  9. linux 源码安装mysql 5.5
  10. 使用XLinq.XElement读取带Namespace(命名空间)的XML
  11. Jupyter 同时支持python2、python3 kernel
  12. 《基于卷积神经网络(CNN)的网络流量分类》优秀本科毕设实验总结
  13. 最强蜗牛服务器维护祷告攻略,最强蜗牛维护补偿在哪里领取 最强蜗牛维护补偿领取攻略_游戏堡...
  14. PHP开发微信商家转账到零钱接口
  15. char ch= 中 什么意思java_问一下java里的char到底是什么
  16. expected an indented block报错的原因
  17. 阿迪卫衣79/茅台茅韵两瓶149/南极人内裤4条14.9/八杯水九件套59.9
  18. 费雪MOGAFX方程式是什么?(二) -
  19. 机械手臂c语言如何编程,一种串联机械手臂的示教编程方法
  20. Ubuntu+Sendmail+Dovecot+Openwebmail 邮件服务器搭建完全解决方案

热门文章

  1. WinCE程序的几种开发方法
  2. windows server 2003 出错提示请求的资源在使用中解决方案
  3. 如何在android studio中设置sdk path?
  4. python模块学习之glob模块
  5. maven GroupId 和ArtifactId的含义
  6. Atitit.web预览播放视频的总结
  7. 基于nginx实现缓存功能及uptream模块详细使用方法
  8. iptables的SNAT和DNAT应用
  9. SUSE Linux启动过程执行脚本顺序
  10. 节选—Android 视频直播 ( 从快播到直播,从高清到无码 )十年视频开发项目