Python3 幾個常見問題
阿新 • • 發佈:2018-12-24
1. 編碼問題:
遇到了幾個字串轉換問題,總結如下:
# str to bytes
str.encode(s)
# bytes to str
bytes.decode(b)
判斷編碼方式可用chardet模組的chardet.detect(content)來協助。
2. char *有地址取內容:
strcontent = string_at(addr, -1)
3. 從動態連結庫中獲取函式並呼叫ctypes
from ctypes import *
dll = CDLL("YourAPP.dll")
dll.YourFunction()
4. 從dll中呼叫c程式,返回char*的情況處理
本來在32位下用string_at就可以解決,但是換成64位後記憶體訪問出錯。所以改用restype,終於解決。
#32位可行,64位出錯:
result = dll.function()
result = string_at(result, -1)
print(result)
#後來改成用restype,32位/64位通用
dll.function.restype = c_char_p
result = dll.function()
print(result)