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 1114909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:04:37+00:00 2026-05-17T03:04:37+00:00

I have a ctypes structure. class S1 (ctypes.Structure): _fields_ = [ (‘A’, ctypes.c_uint16 *

  • 0

I have a ctypes structure.

class S1 (ctypes.Structure):
    _fields_ = [
    ('A',     ctypes.c_uint16 * 10),
    ('B',     ctypes.c_uint32),
    ('C',     ctypes.c_uint32) ]

if I have X=S1(), I would like to return a dictionary out of this object: Example, if I do something like: Y = X.getdict() or Y = getdict(X), then Y might look like:

{ 'A': [1,2,3,4,5,6,7,8,9,0], 
  'B': 56,
  'C': 8986 }

Any help ?

  • 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-05-17T03:04:38+00:00Added an answer on May 17, 2026 at 3:04 am

    Probably something like this:

    def getdict(struct):
        return dict((field, getattr(struct, field)) for field, _ in struct._fields_)
    
    >>> x = S1()
    >>> getdict(x)
    {'A': <__main__.c_ushort_Array_10 object at 0x100490680>, 'C': 0L, 'B': 0L}
    

    As you can see, it works with numbers but it doesn’t work as nicely with arrays — you will have to take care of converting arrays to lists yourself. A more sophisticated version that tries to convert arrays is as follows:

    def getdict(struct):
        result = {}
        for field, _ in struct._fields_:
             value = getattr(struct, field)
             # if the type is not a primitive and it evaluates to False ...
             if (type(value) not in [int, long, float, bool]) and not bool(value):
                 # it's a null pointer
                 value = None
             elif hasattr(value, "_length_") and hasattr(value, "_type_"):
                 # Probably an array
                 value = list(value)
             elif hasattr(value, "_fields_"):
                 # Probably another struct
                 value = getdict(value)
             result[field] = value
        return result
    

    If you have numpy and want to be able to handle multidimensional C arrays, you should add import numpy as np and change:

     value = list(value)
    

    to:

     value = np.ctypeslib.as_array(value).tolist()
    

    This will give you a nested list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is inconsistent: from ctypes import * class S(Structure): _fields_ = [(x, POINTER(c_int)), (y,
I have a .txt file like: Symbols from __ctype_tab.o: Name Value Class Type Size
have written this little class, which generates a UUID every time an object of
Is it possible to modify the _fields_ definition of the ctypes.Structure after it's been
I have a Structure (in this case is a Netlink Msg Header) that I
I cannot seem to implement offsetof for a structure in ctypes. I have seen
I'm using ctypes and I've defined this struct in order to pass parameters class
I have a buffer like: bufstr = ctypes.create_string_buffer(Test, 32) How I obtain the address
I have a ctypes wrapper for a library. Unfortunately, this library is not 100%
I have some ctypes bindings, and for each body.New I should call body.Free. The

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.