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 73876 articles. 14 followers. Post

4 Replies
126 Views

Similar Articles

[PageSpeed] 52


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

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...

show array of array of array of array...
I'm trying to print an array of (datas and array of (datas and array of (datas and array..... Here is the code: ...

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 ...

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...

Re: show array of array of array of array...
sorry..the code: function printtree($array, $level){ while(list($k,$v) = each($array)) if(is_array($k)){ print("$k -><br>"); printtree($k,$level++); } else { for($i=0;$i<$level;$i++) print("&nbsp;"); print("$k : $v<br>"); } } but it does only print the first level and get out !!! I can't figure what's wrong.... Please help !!!! BoB Bob Bedford wrote: > sorry..the code: > > function printtree($array, $level){ > while(list($k,$v) = each($array)) > if(is_array($k)){ > print(&quo...

const and array of array (of array ...)
Imagine I have an array of arrays of ints and want to sum all the ints. #include <stdio.h> int sumints(int arr[3][3]) { int c, r, s=0; for (r=0; r<3; r++) { for (c=0; c<3; c++) { s += arr[r][c]; } } return s; } int main(void) { int my_array[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; printf("sum of ints is %d\n", sumints(my_array)); return 0; } I tried to define the su...

Arrays Of Arrays: Is it an Array or Scalar?
I have a function that I'm using to perform operations on strings in an array. There are times where I'd like to have this function work on arrays of arrays. Is there a simple way to tell if the value of an element in an array is a scalar, or is, instead, a reference to another array? I know I can use a regex to see if the string matches the pattern for an array reference, but is there a more "elegant" and easier way to do it? (And, while I'm at it, is there a way to check for a hash as well? I'd think they'd both be done the same way.) Thanks! Hal In <...

Length of Array of Array of Array
How to find the length of an "Array of Array of Array" I get syntax error when i say $#fx[0][0][0] X-Ftn-To: Tom sudhirx@gmail.com (Tom) wrote: >How to find the length of an "Array of Array of Array" > >I get syntax error when i say $#fx[0][0][0] This should give you the latest index from third level array, $#{ $fx[0][0] } -- Matija Tom <sudhirx@gmail.com> wrote: > How to find the length of an "Array of Array of Array" > > I get syntax error when i say $#fx[0][0][0] Well first off, the $#array syntax does NOT give you the leng...

An array of arrays
My problem is I need to create a loop which will store 40 images in separate arrays. Here is my code: for n=[1:1:40] if n<10 A1='subFolder\photo000'; elseif n>9 A1='subFolder\photo00'; end A2=num2str(n); A3='.jpg'; fileDir=strcat(A1,A2,A3); rgb_img = imread(fileDir); end The problem is I want a range of arrays like: rgb_img1 rgb_img2 rgb_img3 .... rgb_img40 Do I need to use a 3 dimensional array for this. I am genuinely confused. :) Cheers, Chris "Chris " <chris.taeni@gmail.com> wrote in message news:i5ms2l$652$1@fre...

convert array to array-of-arrays?
Rubies: Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]? I had figured a variation on Array#transpose would be available, but I can't find one! -- Phlip On Feb 4, 10:45 am, Phlip <phlip2...@gmail.com> wrote: > Rubies: > > Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]? > > I had figured a variation on Array#transpose would be available, but I can't > find one! > > -- > Phlip have a look at enum.partition (http://ruby-doc.org/core/classes/ Enumerable.html#M003161) cheers Chris Hu...

How to separate array from array of arrays
Hello I don't know if what I wrote in subject makes any sense. I have an array in which there are two entries, which are again arrays (one is numbers and one is text). Like I have entry called NEWDATA1 in workspace and in NEWDATA1 , there are entries called 'data' and 'textdata'. I want to separate 'data' (which is a column of numbers) from NEWDATA1. I actually imported a file (concfromcmg1.txt) which looks like Global Mole Fraction(SOL) 2000-01-01 1,1,1’ *SRF-FORMAT 0 Global Mole Fraction(SOL) 2000-01-03 1,1,1’ *SRF-FORMAT 0.705627 ...

Multidimensional arrays and arrays of arrays
Hello How can I distinguish if an array is multidimensional of if it just contains an array? Or put it another way how can I tell at runtime whether a/b in the example below is an array containing an array, or if it is a multidimensional array. If I know beforehand exactly which type it is, I can use instanceof (see example), but I don't (especially the number of dimensions, I don't know). Else I can call getClass().getName() on the object and see if it starts with "[[" (pretty ugly IMHO). Is there another, cleaner method to do this? public static void main(String[] args) { ...

array of array
Hi, I need to create something like a matrix, say 32 bits on each row and up to 4 rows. That's why on my Spartan3 I have to receive 32 bits word from a serial port and I have to store them (I know I can just place a 32x4 bits long vector and store the data in the same row, but I'd like to learn more about array of array). I wrote: type row is array (cmd_buffer_word downto 0) of std_logic; type cmd_buffer is array (cmd_buffer_depth downto 0) of row; now, how could I transfer the whole 32 bits data inside a vector called cmd_tmp_buffer into the second row of my cmd_buffer? This follow...

array of array
I've to save in an array of array some values. Problem: I don't know the size of the array before filling it. I'd like something like $bigarray = array(): then runnig the code I must create dinamically (I don't know how many) few arrays in the bigarray; $bigarray["X"] = array(): $bigarray["Y"] = array(): then fill those arrays with few unknow number of values. array_push($bigarray["X"],$value1); array_push($bigarray["X"],$value2); array_push($bigarray["Y"],$value5); .... but array_push doesn't manage it. How to do so...

array in array??
awk ' BEGIN{FS=OFS=":"} {split($4,a,",") for(i in a)u[a[i]] for(i in u){x=x","i;delete u[i]} print $1,$2,$3,substr(x,2);x="" }' /etc/group for(i in a)u[a[i]]----------------------------------------------I could not understand this line This means we can an array in an array?? nag wrote: > > for(i in a)u[a[i]] > > I could not understand this line > > This means we can an array in an array?? Yes. In the above example, the values of array a are used as subscripts of array u. See also: http://www.gnu.org/software/gawk/...

array of...array....
I've this structure in a XML file: Type Model Detail Datas Datas Datas Datas /Detail Detail Datas Datas Datas /Detail /Model /Type I've parsed all the stuff, by putting everything in an array. Now, I've a problem as I must use datas in "Detail" tag every time they are processed, but I must first process datas in Model, so I have to wait until Model tag is closed. For now I put everything in an array called $datas (where I store the couple key/valu...

array or not array?
Hi all, i'm a real noob in databasedesign, so i have some questions about it. In case i want to make a booklist with two tables and the following fields: owner_table has the fields ID, name, books book_table has the fields ID, author, title, isbn normally i would say, this is easy, just filling both tables with data and linking them together maybe like owner_table.books as a foreign key linked to book_table.isbn or book_table.ID. In this way i will have many datasets in owner_table with the same entry in 'name', depending on how much books this man (or women) has. ...

When is an array not an array?
Hi, folks. I've encountered what seems to me to be something of an oddity while playing around with XML parsing in PHP, and I wondered if any of you might be able to clear up my confusion... Here's a little code: $xmlDoc = new DOMDocument(); $xmlDoc->load('widget_data.xml'); $widgets = $xmlDoc->getElementsByTagName('widget'); My understanding was that '$widgets' is an array of elements, and the following 'foreach' iterates through that array (this works): foreach ($widgets as $widget) { .... } However, I get an error if I try to access ...

Array in Array
Hi guys, I have a question dealing with arrays. Here it go: Suppose A is a 3 by 1 array, I want to assign A (1,1) a 5 by 1 array B, can I do this? How? Thanks in advance! "George " <guanjihou@gmail.com> wrote in message <hutpgd$km$1@fred.mathworks.com>... > Hi guys, > > I have a question dealing with arrays. Here it go: > > Suppose A is a 3 by 1 array, I want to assign A (1,1) a 5 by 1 array B, can I do this? How? > > Thanks in advance! Hi George, make A a cell array A = cell(3,1); B = randn(5,1); A{1} = B; Wayne % Us...

Array of arrays
Hello, How do I define an array of arrays ? Suppose T is an array of 3 elements. And I want T[0] = intarr(2,5,4), T[1] = intarr(6,5,4) and T[2] = intarr(7,8,4). How do I declare T ? Francois. Fritz writes: > How do I define an array of arrays ? > > Suppose T is an array of 3 elements. > And I want T[0] = intarr(2,5,4), T[1] = intarr(6,5,4) and T[2] = > intarr(7,8,4). > > How do I declare T ? As a pointer array. :-) t = PtrArr(3) t[0] = Ptr_New(intarr(2,5,4)) t[1] = Ptr_New(intarr(6,5,4)) t[3] = Ptr_New(intarr(7,8,4)) (*t[1])[0,*,2] = Indgen(...

Array of Array
Hello, I am wondering if a three-dimensional arrays: array3d_1, array3d_2, array3d_3. Is it wise If pass this to a function/subroutine using array constructor to create four-dimensional array on the fly, such as: call mysubroutine ( (/array3d_1, array3d_2, array3d_3/) ) ? Will that be expensive, or is there better way to do it ? I am also not certain if this on-the-fly array construction will be the first or the last dimension. Thanks for any help. RDB reubendb <reubendb@gmail.com> wrote: > I am wondering if a three-dimensional arrays: > array3d_1, array3d_2, array3d_3. > Is...

array of arrays
i have a database with some 10 records each containing 2 columns, sno,name,city. I want to push this into an array of arrays. i see that Ruy doesnt support multi-dimensional arrays. Any ideas of how to do it? -- Posted via http://www.ruby-forum.com/. Hi -- On Sat, 21 Jul 2007, Divya Badrinath wrote: > i have a database with some 10 records each containing 2 columns, > sno,name,city. > > I want to push this into an array of arrays. > i see that Ruy doesnt support multi-dimensional arrays. > Any ideas of how to do it? Just use arrays as array elements: [ [1,2,3], [4,5,...

array of array?
Hi, there, I wonder, if that is possible to have a array of arrays, for example A(1)=a1(1:N,1:M); A(2)=a2(1:N,1:M); A(3)=a3(1:N,1:M); � If no such thing, is there any simple method to do the same things as dimension A(i,j,k) in Fortran? Could any one please input anything? Thanks hnliuhy wrote: > > > Hi, there, > I wonder, if that is possible to have a array of arrays, for > example > A(1)=a1(1:N,1:M); > A(2)=a2(1:N,1:M); > A(3)=a3(1:N,1:M); > � > > If no such thing, is there any simple method to do the same things > as > dimension > A(i,j,k) in Fo...

Array of array
Hi friends, Please kindly assist me with an answer to this question: Is it possible to create a Matlab array whose elements are in turn 3 element arrays? I mean, how can I create an array as A = {(1,2,2), (3,2,1), (5,3,4), etc}. If possible, how do I access the elements of this array. If not, could this be possible in Fortran? Thanks. On 12/25/2012 8:14 PM, CHINEDU wrote: > Hi friends, > Please kindly assist me with an answer to this question: > > Is it possible to create a Matlab array whose elements are in turn 3 element arrays? > I mean, how can I create an arr...