f



hex array to array of 16 bit numbers?

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!

Todd
0
piir (66)
12/25/2003 10:06:02 AM
comp.lang.python 77021 articles. 3 followers. Post Follow

4 Replies
341 Views

Similar Articles

[PageSpeed] 22

[posted and mailed]

piir@earthlink.net (Todd Gardner) wrote in
news:9b849915.0312250206.61d89841@posting.google.com: 

> 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!
> 
> Todd

First I think your missing the fact that the 'hex' values you're seing is 
simply an encoding of the byte value at that byte-index in the buffer, ie. 
your buffer starts with bytes containing the following values:

  [205] [0] [206] ...

besides that, you're looking for the struct module :-)

-- bjorn
0
12/25/2003 11:02:19 AM
That's all I needed to make it work.

Thank you for pointing me to the correct module.

Todd
0
piir (66)
12/26/2003 7:31:57 PM
Todd Gardner <piir@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@pobox.com
0
rmunn1 (50)
12/26/2003 8:21:04 PM
Todd Gardner wrote in message
<9b849915.0312261131.1344f5d4@posting.google.com>...
>That's all I needed to make it work.
>
>Thank you for pointing me to the correct module.
>
>Todd

Just as a heads up, your code uses the name "buffer", which clobbers the
builtin function "buffer." (Which, honestly, I've never seen used, but
still....)

0
12/27/2003 12:03:58 AM
Reply: