ctypes and unsigned char* - Python

This is a discussion on ctypes and unsigned char* - Python ; Hi, can anybody with ctypes experience tell me, how to handle a C function that returns an unsigned char*? Obviously it is not a restype of c_char_p. Best regards, Oliver...

+ Reply to Thread
Results 1 to 6 of 6

ctypes and unsigned char*

  1. Default ctypes and unsigned char*

    Hi,

    can anybody with ctypes experience tell me, how to handle a C function
    that returns an unsigned char*? Obviously it is not a restype of
    c_char_p.

    Best regards,
    Oliver


  2. Default Re: ctypes and unsigned char*

    oliver.andrich schrieb:
    > Hi,
    >
    > can anybody with ctypes experience tell me, how to handle a C function
    > that returns an unsigned char*? Obviously it is not a restype of
    > c_char_p.


    From the docs:

    c_ubyte

    Represents the C unsigned char datatype, it interprets the value as
    small integer. The constructor accepts an optional integer initializer;
    no overflow checking is done.



    Diez

  3. Default Re: ctypes and unsigned char*

    On 19 ago, 14:44, "oliver.andr..."
    <oliver.andr...> wrote:
    > Hi,
    >
    > can anybody with ctypes experience tell me, how to handle a C function
    > that returns an unsigned char*? Obviously it is not a restype of
    > c_char_p.


    Being signed or unsigned is not important here. But you have to
    disambiguate "returns an unsigned char*"
    - the function returns a string of bytes (unsigned char), null-
    terminated.
    - the function returns a pointer to a single byte.

    In the first case, use a plain c_char_p - the individual "chars" are
    already unsigned in Python (that is, ord(xxx[i]) is always positive)
    In the second case, first define the pointer type:

    c_ubyte_p = POINTER(c_ubyte)
    your_function.restype = c_ubyte_p

    --
    Gabriel Genellina


  4. Default Re: ctypes and unsigned char*

    On 19 Aug., 20:40, Gabriel Genellina <gagsl-...@yahoo.com.ar> wrote:
    > In the first case, use a plain c_char_p - the individual "chars" are
    > already unsigned in Python (that is, ord(xxx[i]) is always positive)
    > In the second case, first define the pointer type:
    >
    > c_ubyte_p = POINTER(c_ubyte)
    > your_function.restype = c_ubyte_p


    I will have to look into that. My function I want to wrap has the
    following signature

    unsigned char *MagickGetImageProfile(MagickWand *wand,const char
    *name,
    size_t *length)

    Well, the first argument and the second are straight forward, and work
    as expected. The last argument also works and it should take up the
    length of the string (unsigned char *). If I now do

    dll.MagickGetImageProfile.restype = c_char_p
    result = dll.MagickGetImageProfile(wand, "8bim", byref(length))

    result is either an empty string or a 3 bytes long string, while
    length containes the correct length of 1086 bytes for example. I also
    tried

    dll.MagickGetImageProfile.restype = POINTER(c_ubyte)

    with the same result. But maybe I should do some further debugging....

    Best regards,
    Oliver


  5. Default Re: ctypes and unsigned char*

    En Mon, 20 Aug 2007 08:30:50 -0300, oliver.andrich
    <oliver.andrich> escribi�:

    > On 19 Aug., 20:40, Gabriel Genellina <gagsl-...@yahoo.com.ar> wrote:
    >> In the first case, use a plain c_char_p - the individual "chars" are
    >> already unsigned in Python (that is, ord(xxx[i]) is always positive)
    >> In the second case, first define the pointer type:
    >>
    >> c_ubyte_p = POINTER(c_ubyte)
    >> your_function.restype = c_ubyte_p

    >
    > I will have to look into that. My function I want to wrap has the
    > following signature
    >
    > unsigned char *MagickGetImageProfile(MagickWand *wand,const char
    > *name,
    > size_t *length)
    >
    > Well, the first argument and the second are straight forward, and work
    > as expected. The last argument also works and it should take up the
    > length of the string (unsigned char *). If I now do


    So, if I understand correctly, after the call, *length will contain the
    length of the buffer pointed by the function result.

    > dll.MagickGetImageProfile.restype = c_char_p
    > result = dll.MagickGetImageProfile(wand, "8bim", byref(length))
    >
    > result is either an empty string or a 3 bytes long string, while
    > length containes the correct length of 1086 bytes for example. I also
    > tried


    If the result can contain nul bytes, you have to specify its size
    explicitely (else it will be truncated at the first zero). Use the
    string_at utility function:

    result_str = string_at(result, length)

    (That should work fine, I presume...)

    --
    Gabriel Genellina


  6. Default Re: ctypes and unsigned char*

    On 23 Aug., 05:46, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
    > If the result can contain nul bytes, you have to specify its size
    > explicitely (else it will be truncated at the first zero). Use the
    > string_at utility function:
    >
    > result_str = string_at(result, length)
    >
    > (That should work fine, I presume...)


    Thank a lot, that does exactly what I expected it to do.

    Best regards,
    Oliver


+ Reply to Thread

Similar Threads

  1. char *, unsigned char * and POD types
    By Application Development in forum c++
    Replies: 12
    Last Post: 12-15-2007, 11:59 AM
  2. Printing the range s of unsigned char and unsigned int.
    By Application Development in forum C
    Replies: 20
    Last Post: 09-20-2007, 01:03 AM
  3. convertion problem? from char* to unsigned char*?
    By Application Development in forum c++
    Replies: 2
    Last Post: 09-06-2007, 10:17 AM
  4. Converting a unsigned char * to const char *
    By Application Development in forum c++
    Replies: 12
    Last Post: 08-04-2007, 09:56 PM
  5. complement of an unsigned char
    By Application Development in forum C
    Replies: 13
    Last Post: 05-11-2006, 12:17 AM