printf
返回 1 而不是“Hello World!”这就是想要的结果。
我用谷歌搜索了一下,认为这是因为序列处理方式的变化。
如何修改代码以打印“Hello World!”?
www.mail-archive.com/[email protected]/msg15119.html
import ctypes
msvcrt=ctypes.cdll.msvcrt
string=b"Hello World!"
msvcrt.printf("%s", string)
最佳答案
第一个参数也必须是字节字符串:
msvcrt.printf(b"%s", string)
printf 的返回值是打印的字符数,本例中应为 12。
编辑:
如果您希望返回字符串而不是打印字符串,可以使用sprintf
。这是危险的,不推荐。
s = ctypes.create_string_buffer(100) #must be large enough!!
msvcrt.sprintf(s, b'%s', b'Hello World!')
val = s.value
我不知道你为什么要这样做,因为 Python 有自己的字符串格式。 sprintf
是一种危险的方法,因为它容易发生缓冲区溢出。
关于Python3k ctypes printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2636597/