hex array to array of 16 bit numbers?

Robin Munn rmunn at pobox.com
Fri Dec 26 15:21:04 EST 2003


Todd Gardner <piir at earthlink.net> wrote:
> Hello everyone,
>
> I would really appreciate some of your expertise or even just
> pointers.  How do I convert this hex array variable "buffer" to array
> of 16 bit numbers?
>
>>>> 
>>>> from ctypes import *
>>>> buffer = c_buffer(8)
>>>> status = ni.DAQ_Op (deviceNumber, chan, gain, buffer, count,
> sampleRate)
>>>> print "repr(buffer.raw) =", repr(buffer.raw)
>
> repr(buffer.raw)= '\xcd\x00\xce\x00\xce\x00\xce\x00'
>>>> 
>
> Any ideas would be greatly appreciated!

I think the struct.unpack() method is what you're looking for here:

    http://www.python.org/doc/current/lib/module-struct.html

If you've got four 16-bit numbers, then:

    import struct
    status_tuple = struct.unpack('4h', buffer.raw)

should give you what you want. Read the documentation for the meaning of
the various format characters.

-- 
Robin Munn
rmunn at pobox.com




More information about the Python-list mailing list