Python type corresponding to type C

Type comparison table

ctypes type C type Python type
c_bool _Bool bool (1)
c_char char 1-character bytes object
c_wchar wchar_t 1-character string
c_byte char int
c_ubyte unsigned char int
c_short short int
c_ushort unsigned short int
c_int int int
c_uint unsigned int int
c_long long int
c_ulong unsigned long int
c_longlong __int64 or long long int
c_ulonglong unsigned __int64 or unsigned long long int
c_size_t size_t int
c_ssize_t ssize_t or Py_ssize_t int
c_float float float
c_double double float
c_longdouble long double float
c_char_p char * (NUL terminated) bytes object or None
c_wchar_p wchar_t * (NUL terminated) string or None
c_void_p void * int or None
  • Between char and wchar, one is C-type single-byte encoding and the other is Unicode two-byte encoding, which can usually be converted to each other by decode and encode.
  1. Normal type assignment

    Since python is untyped, once an object is created and assigned, assigning it again will become a new type. So assignments are usually made in the form of classes or containers.That is, assign values by adding attributes.

    Normal type assignment

    	from ctypes import *
    	a = c_int(2)
    	print(type(a))
    	a = 2
    	print(type(a))
    	a = c_int(2)
    	a.value = 3
    	print(a)
    	"""
    	<class 'ctypes.c_long'>
    	<class 'int'>
    	c_long(3)
    	[Finished in 0.1s]
    	"""
    

    Mismatch type assignment

    If assignment is made to a non-corresponding type, it is first converted to the corresponding type, analogy C.

  2. String Creation

    • C-style string

      ctypes.create_string_buffer(init_or_size, size=None) Create a string buffer that can be modified, like char buffer[size]={init_or_size} The init_or_size must be of type int (indicating the creation size) or bytes for initialization.

      	from ctypes import *
      	buf = create_string_buffer(10)
      	print(sizeof(buf),buf,type(buf),buf.raw,buf.value)
      	buf = create_string_buffer(b"test",10)
      	print(sizeof(buf),buf,type(buf),buf.raw,buf.value)
      	buf = create_string_buffer(b"test")
      	print(sizeof(buf),buf,type(buf),buf.raw,buf.value)
      	"""
      	10 <ctypes.c_char_Array_10 object at 0x037A9460> <class 'ctypes.c_char_Array_10'> b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b''
      	10 <ctypes.c_char_Array_10 object at 0x03835100> <class 'ctypes.c_char_Array_10'> b'test\x00\x00\x00\x00\x00\x00' b'test'
      	5 <ctypes.c_char_Array_5 object at 0x037A9460> <class 'ctypes.c_char_Array_5'> b'test\x00' b'test'
      	[Finished in 0.1s]
      	"""
      
    • Unicdoe style

      ctypes.create_unicode_buffer(init_or_size, size=None) Create a character array buffer of Unicode type corresponding to a Python type c_wchar "ass" common type string and a C type wchar.

      	from ctypes import *
      	buf = create_unicode_buffer(10)
      	print(sizeof(buf),buf,type(buf),buf.value)
      	buf = create_unicode_buffer("test",10)
      	print(sizeof(buf),buf,type(buf),buf.value)
      	buf = create_unicode_buffer("test")
      	print(sizeof(buf),buf,type(buf),buf.value)
      	"""
      	20 <ctypes.c_wchar_Array_10 object at 0x00AB94F0> <class 'ctypes.c_wchar_Array_10'> 
      	20 <ctypes.c_wchar_Array_10 object at 0x02B15100> <class 'ctypes.c_wchar_Array_10'> test
      	10 <ctypes.c_wchar_Array_5 object at 0x00AB94F0> <class 'ctypes.c_wchar_Array_5'> test
      	[Finished in 0.1s]
      	"""
      
  3. Types that can be used generically.

    There are several types that can be used directly like the C type, such as int, string,bytes, which can be compared with the above types.All matches can be used directly.

  4. Custom types as parameters.

Keywords: Python encoding

Added by Bhoomika on Sat, 23 Nov 2019 14:54:08 +0200