hex array to array of 16 bit numbers?

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply piir (66) 12/25/2003 10:06:02 AM

See related articles to this posting

[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
Reply bjorn.pettersen (24) 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
Reply 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
Reply 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
Reply francisgavila (144) 12/27/2003 12:03:58 AM
comp.lang.python 71920 articles. 11 followers. Post

4 Replies
50 Views

Similar Articles

[PageSpeed] 52

  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Convert varying hex string to array of 8 and 16 bit numbers
Hi &nbsp; I have a data string that I'm reading from a file.&nbsp; The format of the string is as follows: &nbsp; a bB cC dD eE fF gG hH iI j kK/r &nbsp; each letter is an 8 bit character.&nbsp; The capitol letters are high bit of a 16 bit number. &nbsp; I have a sample that works.&nbsp; It converts all the characters to an array with eleven rows.&nbsp;&nbsp; My problem is my files usually have a million data points or more and it takes a while for my VI to execute.&nbsp; &nbsp; I've attached a sample file and an example VI with 3 possible ...

How to copy an 8 bits array into a 16 bits array?
Hi, I need a fast code which copies an 8 bits array, into a 16 bits array. I have now: char* src; short* dst; int i,size; for( i=0;i < size; i++) dst[i]=(short)src[i]; I am not familiar with pentium assembly. Any suggestions? thanks, Moshe On Thu, 16 Dec 2004 20:33:12 +0000 (UTC), spamtrap@crayne.org wrote in comp.lang.asm.x86: > Hi, > I need a fast code which copies an 8 bits array, into a 16 bits array. > I have now: > > char* src; > short* dst; > int i,size; > > for( i=0;i < size; i++) > dst[i]=(short)src[i]; ^^^^^^^ This is a redundant...

Convert ctypes 16 bit c_short array to a 32 bit numpy array
I'm using ctypes to have a dll fill a buffer with 16 bit data. I then want to convert this data to a numpy array. The code snippet below converts the data from 16 bit to 32 bit, but two 16 bit numbers are concatenated to make a 32 bit number and half the array is zero. Buffer = (c_short * byteSize)() self.cam.Qframe.pBuffer = cast(pointer(Buffer), c_void_p) perr = self.cam.GrabFrame() image1 = np.frombuffer(Buffer, int) xdim = self.cam.Qframe.width ydim = self.cam.Qframe.height image2 = image1.reshape(xdim, ydim) imag...

converting 8 bit image array to 16 bit?
Hi there.... i've got an structure wich is like an pointer to 320*240 array wich is 8 bit image... i've got also stored its color pallate in some other structure.... i was willing to port this to another platform and like to use it under 16 bit color display.... i only know that i have an address for start of that 16 bit memory address to put values in...but i dont know how to convert those 8 bit indexed colors to 16 bit? Thanks Roozbeh GHolizadeh wrote: > Hi there.... > > i've got an structure wich is like an pointer to 320*240 array wich is > 8 bit image... >...

Hex String to Number to Array... I think...
Hello to the group, &nbsp; Here's my dillemma... I'm interested in a specific output from my device. The expected output is 256&nbsp;values, as a row of 512 byte (Hex). To simplify communication, I used the LabVIEW Serial VI and added a terminal at the "Read String". This outputs a string (when displayed as "Hex Display") like 0200 0200 ... ...&nbsp;0200 0100. I wired that output into the "String" input of a Hexidecimal String to Number conversion. Then, I am using a&nbsp;Build Array, wired into the single element. Finally,&nbsp;the app...

save 16 bit array as image
Hello, I am a new labview user. I am trying to save an image which is being correctly displayed on the front panel of my program as an intensity graph. I&nbsp; am able to get a 2D array of the pixel values (using the variant to data function, the camera I use has an Active X interface with labview). The camera allows 16 bit or 12 bit digitization options. I would like to save the image pizel values as a 16 bit BMP or TIFF file. However, from what I understand, the flatten pixmap option in labview does not support 16 bit images. How do I go about saving my images? Any feedback would be ext...

bit numbers in packed arrays of Boolean
I was pleasantly surprised to discover that given this code compiled with GNAT 6.2.1 for an Intel processor: subtype Bit_Index_16_Type is Integer range 0 .. 15; type Bit_Array_16_Type is array (Bit_Index_16_Type) of Boolean; pragma Pack (Bit_Array_16_Type); for Bit_Array_16_Type'Size use 16; function To_Bit_Array is new Ada.Unchecked_Conversion (Source => Interfaces.Unsigned_16, Target => Bit_Array_16_Type); Word : constant Interfaces.Unsigned_16 := 2#1000_0000_0000_0000#; Bits : constant Bit_Array_16_Type := To_Bit_Array (Word); the index of Bit_...

What is the max number of bits a boolean array can have?
Hello, &nbsp; &nbsp;&nbsp; What is the maximum number of bit that a boolean array can have? &nbsp; Regards, &nbsp; &nbsp; Kaspar ...

Append each bit to the binary number (Not Array Indicator)
Hi All &nbsp; If i have the 8 Number control. Each Control represent each bit. i.e. 0th control bit="1", 1th control bit="0", other bit = "0"....7th control bit ="1". How to output the 1 binary number indicator. --&gt;10000001 &nbsp; Thanks &nbsp; Best Regards,Steve I had solved it. &nbsp; i use true/false case to multiply each bit interial. i.e. For&nbsp;8 th bit. If =0, output to 0, if =1 (i.e not equal to 0), output 10000000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For 7 th bit. If =0, output to 0, if =1 (i.e not equ...