Python ctypes 數據類型
Python ctypes 數據類型
節錄自官網 https://docs.python.org/3.6/library/ctypes.html:
C 類型 | Python 類型 | Ctypes 類型 |
char | 1 char string | c_char |
wchar_t | 1 char unicode string | c_wchar |
char | int/long | c_byte |
unsigned char | int/long | c_ubyte |
short | int/long | c_short |
unsigned short | int/long | c_ushort |
int | int/long | c_int |
unsigned int | int/long | c_uint |
long | int/long | c_long |
unsigned long | int/long | c_ulong |
__int64 or long long | int/long | c_longlong |
unsigned __int64 or unsigned long long | int/long | c_ulonglong |
float | float | c_float |
double | float | c_double |
long double | float | c_longdouble |
char * (NUL terminated) | string or None | c_char_p |
wchar_t * (NUL terminated) | unicode or None | c_wchar_p |
void * | int/long or None | c_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 中的結構,下次再跟大家介紹。
留言
張貼留言