Python ctypes 數據類型

Python ctypes 數據類型


節錄自官網 https://docs.python.org/3.6/library/ctypes.html:

C 類型Python 類型Ctypes 類型
char1 char stringc_char
wchar_t1 char unicode stringc_wchar
charint/longc_byte
unsigned charint/longc_ubyte
shortint/longc_short
unsigned shortint/longc_ushort
intint/longc_int
unsigned intint/longc_uint
longint/longc_long
unsigned longint/longc_ulong
__int64 or long longint/longc_longlong
unsigned __int64 or unsigned long longint/longc_ulonglong
floatfloatc_float
doublefloatc_double
long doublefloatc_longdouble
char * (NUL terminated)string or Nonec_char_p
wchar_t * (NUL terminated)unicode or Nonec_wchar_p
void *int/long or Nonec_void_p

一些例子

>>> s = "Hello, World"
>>> c_s = c_wchar_p(s)
>>> print(c_s)
c_wchar_p(139966785747344)
>>> print(c_s.value)
Hello World
>>> c_s.value = "Hi, there"
>>> print(c_s)              # the memory location has changed
c_wchar_p(139966783348904)
>>> print(c_s.value)
Hi, there
>>> print(s)                # first object is unchanged
Hello, World
>>>

個人比較常用的是 ctypes 的 Structure 解析 binary file 中的結構,下次再跟大家介紹。

留言

熱門文章