469,916 Members | 1,972 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

using builtin array

Is it possible to join/append 2 arrays defined with different
typecodes?

What typecode should i use to generate the following output.

data1 = array('h', '\0', 6)
data2 = array('L', '\0', 25)

for i in range( 6):
data1[0] = 0xFF
data2[1] = 0x00
data1[2] = 0x00
data1[3] = 0x00
data1[4] = 0x00
data1[5] = 0x00

for i in range( 2):
data2[0] = 0xF0F0F0F0
data2[1] = 0xFFFFFFFF

Output should be...
(0xFF 0x00 0x00 0x00 0x00 0x00 0xF0F0F0F0 0xFFFFFFFF)

Thank You,
-SB

Jul 19 '05 #1
4 1174
sh********@gmail.com wrote:
Is it possible to join/append 2 arrays defined with different
typecodes?

What typecode should i use to generate the following output.

data1 = array('h', '\0', 6)
data2 = array('L', '\0', 25)

for i in range( 6):
data1[0] = 0xFF
data2[1] = 0x00
data1[2] = 0x00
data1[3] = 0x00
data1[4] = 0x00
data1[5] = 0x00

for i in range( 2):
data2[0] = 0xF0F0F0F0
data2[1] = 0xFFFFFFFF

Output should be...
(0xFF 0x00 0x00 0x00 0x00 0x00 0xF0F0F0F0 0xFFFFFFFF)

An arry has one typecode that applies to all its elements - so what you
want can't be done.

But you can use two lists and the module struct to create a string that
resembles the memory layout you want.
data1 = [0] * 6
data2 = [0L] * 25

struct.pack("b" * len(data1) + "l" * len(25), *(data1 + data2))

But I think you should give us more information on what you actually
want to accomplish, as I've got the impression that you try to force
things in awy that is not optimal.

Diez
Jul 19 '05 #2
I am constructing a packet, with the first 6 bytes being the
destination address, followed by src, and type. The remaining space
will be filled with data. I need to use an array because of the
buffer_info method i am calling.

-SB

Jul 19 '05 #3
as*****@gmail.com wrote:
I am constructing a packet, with the first 6 bytes being the
destination address, followed by src, and type.
As I told you - use struct.
The remaining space
will be filled with data. I need to use an array because of the
buffer_info method i am calling.


That's a circular argument - to use arrays because of a methods arrays
habe. Use lists instead. And if you really need that buffer_info, you
could create a string with struct and then make an array of bytes out of
that.

Arrays are homogenous. No chance concatenating them.
Diez
Jul 19 '05 #4
Arrays are homogenous. No chance concatenating them.


This should of course read "No chance concatenating arrays of different
typecodes".
--
Regards,

Diez B. Roggisch
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Joachim Dahl | last post: by
2 posts views Thread by BJörn Lindqvist | last post: by
6 posts views Thread by Anders K. Olsen | last post: by
3 posts views Thread by amitsoni.1984 | last post: by
20 posts views Thread by Ari Krupnik | last post: by
1 post views Thread by iwl | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.