从DLL返回的char []转换为Python字符串 [英] casting into a Python string from a char[] returned by a DLL

查看:313
本文介绍了从DLL返回的char []转换为Python字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将C样式const char []字符串指针(从DLL返回)转换为Python兼容的字符串类型。但当执行Python27时:

I am attempting to cast a C style const char[] string pointer (returned from a DLL) into a Python compatible string type. but when Python27 executes:

import ctypes

charPtr = ctypes.cast( "HiThere", ctypes.c_char_p )
print( "charPtr = ", charPtr )

我们得到: charPtr = c_char_p('HiThere')

也许某些东西没有得到正确的评估。
我的问题是:

perhaps something is not to be evaluating properly. My questions are:


  1. 一个人应该如何将此charPtr转换回与Python兼容且可打印的字符串?

  2. 是否刚刚提到了强制转换操作应做的事情?


推荐答案

ctypes.cast()用于将一个ctype实例转换为另一种ctype数据类型。
不需要它即可将其转换为python字符串。
只需使用 .value即可在python字符串中获取它。

ctypes.cast() is used to convert one ctype instance to another ctype datatype. You don't need it To convert it to python string. Just use ".value" to get it in python string.

>>> s = "Hello, World"
>>> c_s = c_char_p(s)
>>> print c_s
c_char_p('Hello, World')
>>> print c_s.value
Hello, World

更多信息此处

这篇关于从DLL返回的char []转换为Python字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆