[Python] Converting between bytes and POINTER(c_ubyte)

Discussão em 'Python' iniciado por Stack, Julho 20, 2025.

  1. Stack

    Stack Membro Participativo

    How do I convert between bytes and POINTER(c_ubyte) in Python?

    I want to pass a bytes object to a C function as a POINTER(c_ubyte) argument, and I want to work with a returned POINTER(c_ubyte) as bytes.

    Right now I am using:

    data = b'0123'
    converted_to = ctypes.cast(data, ctypes.POINTER(ctypes.c_ubyte))
    converted_from = bytes(converted_to)


    This doesn't seem quite right. I get a warning in PyCharm on data in the converted_to line that says:


    Expected type 'Union[_CData, _CArgObject]', got 'bytes' instead
    ​

    Continue reading...

Compartilhe esta Página