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...>