Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:54:59+00:00 2026-06-01T15:54:59+00:00

Using the ctypes module I can easily import a POINTER(c_char) or a c_char_p type

  • 0

Using the ctypes module I can easily import a POINTER(c_char) or a c_char_p type into python, but neither of these provides a way to end up with a python string that contains zero value bytes.

c_char_p is zero terminated, meaning that a char * array from C is terminated at the first zero value.

POINTER(c_char) is the recommended way of importing binary data that can have 0 values, but there doesn’t seem to be a way to directly convert this into a python string.

I can do this:

pixels = clibblah.get_pixels()
a = ""
for i in range(0, clibblah.get_pixel_length()):
    a += pixels[i]

…but this 1) doesn’t seem very pythony, and 2) takes forever (converting a 640×480 block of pixel data takes about 2 seconds on my mac).

I’ve seen a bunch of questions regarding this on stack overflow, but darned if I can see one that isn’t either people going “why do you need to do that?” or “c_char_p will do what you want” (it doesn’t, as I’ve described above).

The only credible advice I’ve seen is to use the c api PyString_FromStringAndSize, as recommended here:
http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/FAQ.html

Can’t really see how that helps though, because afaik that’s a cython feature, not a python one.

For the interested, the reason I need to do this is I’m working with panda3d and a kinect, and the kinect c api provides an array of unsigned char * values and the panda3d api lovingly provides a setPixels() call that only takes a python string as an argument.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:55:00+00:00Added an answer on June 1, 2026 at 3:55 pm

    As you said, use a POINTER(c_char) to get a pointer to the array of binary data. To put that together into a string, you can just take a slice of it, since array indexing works as expected with ctypes pointers:

    clibblah = ctypes.cdll.LoadLibrary('clibblah.dylib')
    get_pixels = clibblah.get_pixels
    get_pixels.restype = ctypes.POINTER(ctypes.c_char)
    
    pixels = get_pixels()
    num_pixels = clibblah.get_pixel_length()
    
    # Slice the ctypes array into a Python string
    a = pixels[:num_pixels]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm calling a C function using ctypes from Python. It returns a pointer to
I'm accessing a C struct which contains some time_t fields using python ctypes module.
I'm attempting to convert a C header into a Python library using ctypes and
I'm trying to access some C code via Python using ctypes, so I wrote
Currently I am using ctype_alnum to check for alphanumeric type in csv file. But
I am porting visual c++ 6 application to python using ctypes. It uses same
I am working with some C code called from Python using ctypes. Somewhere in
in my python project I call a c++ dll using ctypes library. That c++
I am using a python script to do my webserver using the BaseHTTPServer module.
I am trying to use a dll in Python source using the ctypes. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.