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)
本文介绍了Python中处理字符串编码转换的方法,包括使用str.encode()和bytes.decode()进行编码和解码,利用chardet模块检测编码。同时,文中详细讲述了如何通过ctypes库加载动态链接库(DLL),调用其中的函数,并处理不同位数系统下返回char*类型的问题。

被折叠的 条评论
为什么被折叠?



