using builtin array

Discussion in 'Python' started by shama.bell@gmail.com, Jun 1, 2005.

  1. Guest

    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
     
    , Jun 1, 2005
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Jun 1, 2005
    #2
    1. Advertising

  3. Guest

    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
     
    , Jun 1, 2005
    #3
  4. 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
     
    Diez B. Roggisch, Jun 2, 2005
    #4

  5. > Arrays are homogenous. No chance concatenating them.


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

    Diez B. Roggisch
     
    Diez B. Roggisch, Jun 2, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    442
    Michel de Becdelièvre
    Jun 11, 2005
  2. Joachim Dahl

    extending the builtin array class from C

    Joachim Dahl, Aug 14, 2004, in forum: Python
    Replies:
    0
    Views:
    274
    Joachim Dahl
    Aug 14, 2004
  3. bdb112
    Replies:
    2
    Views:
    311
    Chris Torek
    Jul 2, 2011
  4. Peng Yu
    Replies:
    1
    Views:
    150
    Jürgen Exner
    Dec 31, 2009
  5. Replies:
    6
    Views:
    106
Loading...

Share This Page