Re: Can't create small bitfields
eryksun <[email protected]> Tue, 15 Apr 2014 22:54:45 -0400
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CACL+1auGwCf0SKMYezhTTGFV_972CPXQXN0LM8FucwQgZ-H=FA@mail.gmail.com> |
On Tue, Apr 15, 2014 at 8:01 PM, Rob Gaddi <[email protected]> wrote: > the smallest Structure I can get is 4 bytes. Any ideas on what I'm > doing wrong? > > from __future__ import print_function > from ctypes import * > > class Struct(Structure): > _pack_ = 1 > > Struct._fields_ = [ > ('low', c_uint, 4), > ('high', c_uint, 4), > ] You're using c_uint as the storage unit size. Instead use c_ubyte: from ctypes import * class Struct(Structure): _fields_ = [('low', c_ubyte, 4), ('high', c_ubyte, 4)] x = Struct(low=7, high=15) >>> sizeof(x) 1 >>> (x.high << 4) + x.low 247 >>> list(bytes(x)) [247] ------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/NeoTech