Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总
In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try explicitly making your string a byte string using .encode('ascii')
before handing it to DLL.prepare
.
==>在Python 2.7中,string默认的是byte-strings,而在 Python 3.x中默认的是unicode。因此在3.x中将string传入到调用的DLL之前先使用 .encode('ascii')
将其变成 byte string类型
以下两种方法都能实现 unicode == > byte-strings
>>> a = "hhk"
>>> b = bytes(str(a),'ascii')
>>> b
b'hhk'
>>> c = str(a).encode('ascii')
>>> c
b'hhk'
print(type(a) , type(b) , type(c))
<class 'str'> <class 'bytes'> <class 'bytes'>
可阅读的参考文章 :
“Python3中byte与string的相互转换” : http://www.cnblogs.com/txw1958/archive/2012/08/31/python3-bytes-string.html
1.
c_int()含有属性value,取其value变为python的int类型数据
>>> from ctypes import *
>>> a = c_int(10)
>>> type(a)
<class 'ctypes.c_long'>
>>> b = a.value
>>> b
10
>>> type(b)
<class 'int'>
2.
Python代码中进行比较时,比较的参数一定要全部都是python数据类型,ctypes的数据类型是不能比较的
(python的int类型与ctypes中的c_int类型比较的bool值是False,ctypes中的c_int类型与c_int类型比较的bool值是False)
>>> c = 5
>>> d = c_int(5)
>>> c == d
False
>>> e = d.value
>>> c == e
True
>>> a = c_int(5)
>>> b = c_int(5)
>>> a == b
False
(ctypes中的c_char_p类型与c_char_p类型比较的bool值是False)
>>> a = c_char_p(b"aaa")
>>> b = c_char_p(b"aaa")
>>> a == b
False
>>> a.value == b.value
True
>>> bool(a)
True
>>> a = c_char_p()
>>> bool(a)
False
3.
c_char_p()含有属性value,取其value变为bytes object,再取其decode()属性变为python的str类型数据,python的str类型有encode属性,通过encode属性,变成bytes object类型
>>> a="hhk"
>>> type(a)
<class 'str'>
>>> b=c_char_p(b"hhk")
>>> type(b) b有value属性
<class 'ctypes.c_char_p'>
>>> c=b.value
>>> c bytes object类型,有decode属性
b'hhk'
>>> type(c)
<class 'bytes'>
>>> d=b.value.decode() 通过decode属性,变成python的str类型
>>> d
'hhk'
>>> type(d)
<class 'str'>
>>> b = "hhk" b有encode属性
>>> c = b.encode() 通过encode属性,变成bytes object类型
>>> c
b'hhk'
>>> type(c)
<class 'bytes'>
4.
数字类型的比较:
>>> c = 5
>>> d = c_int(5)
>>> c == d
False
>>> e = d.value
>>> c == e
True
字符串类型的比较:
>>> str1 = "hhk"
>>> str2 = "hhk"
>>> str1 == str2
True
5.
判断c_char_p类型字符串是否为空,使用 if(a)
>>> a = c_char_p()
>>> bool(a)
False
>>> a = c_char_p(b"hhk")
>>> bool(a)
True
判断string类型字符串是否为空,使用 if(a)
>>> a=""
>>> bool(a==None)
False
>>> bool(a)
False
判断列表是否为空,使用 if(list)
>>> list = []
>>> bool(list)
False
>>> not list OK
True
>>> list == None ERROR
False
判断bool值的真假
--ctypes中的任何类型都不能和python中的类型直接比较
>>> a = c_bool(0)
>>> a
c_bool(False)
>>> type(a)
<class 'ctypes.c_bool'>
>>> a==0
False
>>> a==False
False
>>> a.value == 0
True
>>> b = a.value
>>> type(b)
<class 'bool'>
>>> b
False
>>> b == 0
True
6.
判断定义的一个实体对象是否为空
rt = hostid_t()
bool(rt) ==>False
bool(rt == None) ==>False
bool(rt.platform) ==>False
我采用的方法是判断实体对象元素是否为空
if rt.platform == None
if rt.platform
7.
Calling the pointer type without an argument creates a NULL pointer. NULL pointers have a False boolean value
== > 当调用POINTER类型没有传入参数时,将创建一个空指针,空指针将会有一个错误的boolean值
>>> null_ptr = POINTER(c_int)()
>>> print (bool(null_ptr))
False
>>> c_int_p = POINTER(c_int)
>>> null_ptr = c_int_p()
>>> print(bool(null_ptr))
False
//Python 官网实例:
>>> PI = POINTER(c_int)
>>> PI
<class 'ctypes.LP_c_long'>
>>> PI(42)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: expected c_long instead of int
>>> PI(c_int(42))
<ctypes.LP_c_long object at 0x...>
Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总的更多相关文章
- python安装过程中的一些问题
因为看到大神的教程是基于python V2.7,下载该版本且安装成功. 安装目录: https://www.python.org/download/releases/2.7/ 根据系统进行安装包下载 ...
- python学习笔记:安装boost python库以及使用boost.python库封装
学习是一个累积的过程.在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结. 前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C+ ...
- python的库有多少个?python有多少个模块?
这里列举了大概500个左右的库: ! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...
- python 学习第五天,python模块
一,Python的模块导入 1,在写python的模块导入之前,先来讲一些Python中的概念性的问题 (1)模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质是.py ...
- 进击的Python【第二章】:Python基础(二)
Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...
- Python黑帽编程1.3 Python运行时与包管理工具
Python黑帽编程1.3 Python运行时与包管理工具 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and ...
- Python黑帽编程2.1 Python编程哲学
Python黑帽编程2.1 Python编程哲学 本节的内容有些趣味性,涉及到很多人为什么会选择Python,为什么会喜欢这门语言.我带大家膜拜下Python作者的Python之禅,然后再来了解下P ...
- 进击的Python【第一章】:Python背景初探与Python基础(一)
Python背景初探 一.Python起源 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做 ...
- [Python学习] Linux环境下的Python配置,必备库的安装配置
1.默认Python安装情况 一般情况,Linux会预装Python的,版本较低,比如Ubuntu15的系统一般预装的是Python2.7.10. 使用命令:which python可以查看当前的py ...
随机推荐
- MySQL必备命令
来源:http://www.cnblogs.com/liushuijinger/p/3381775.html 今天跟大家分享一下MySQL从连接到具体操作的一系列常用命令.可能有的人觉得现在有很多可视 ...
- AutoHotKey 快速入门
AutoHotKey 是一个免费的键盘宏程序,可以用于配置键盘快捷键.鼠标事件 以及摇杆事件,还可以在输入文本的时候对文本进行扩展(自动补全) 第一个脚本 新建文件test.ahk并输入以下内容: ^ ...
- jQuery的动画与特效
显示与隐藏 show() 和 hide() 方法 动画效果的show() 和 hide() show(speed,[]callback) hide(speed,[]callback) speed:表示 ...
- ASP.NET MVC下使用文件上传和IIS7下的默认设置限制了上传大小的方法
不多说了,直接用别人的 http://www.cnblogs.com/jiekzou/p/4491505.html
- MATLAB中imfilter函数
功能:对任意类型数组或多维图像进行滤波. 用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,...) 或写作g = imfilter(f, w ...
- xsy1436-括号游戏
题目 递归定义括号序列: 空串是括号序列 (A)是一个括号序列,其中A为括号序列 AB是一个括号序列,其中A,B均为括号序列 定义严格括号序列为形如(A)的括号序列,其中A为括号序列. 给出一个长度为 ...
- 转:Conjugate prior-共轭先验的解释
Conjugate prior-共轭先验的解释 原文:http://blog.csdn.net/polly_yang/article/details/8250161 一 问题来源: 看PRML第 ...
- 【刷题】洛谷 P4329 [COCI2006-2007#1] Bond
题意翻译 有 \(n\) 个人去执行 \(n\) 个任务,每个人执行每个任务有不同的成功率,每个人只能执行一个任务,求所有任务都执行的总的成功率. 输入第一行,一个整数 \(n\) ( \(1\leq ...
- [luogu5176] 公约数
题目描述 求 \[ \sum_{i=1}^n\sum_{j=1}^m\sum_{k=1}^p\gcd(i\cdot j,i\cdot k,j\cdot k)\times \gcd(i,j,k)\tim ...
- BZOJ2553 [BeiJing2011]禁忌 【AC自动机 + dp + 矩乘优化】
题目链接 BZOJ2553 题解 话说在前,此题卡精度,最好开long double 先建\(AC\)自动机 求期望,逆着求,设\(f[i][j]\)为长度为\(i\)的串,当前匹配AC自动机\(j\ ...