Re: Unsigned char array to an unsigned longlong array

Discussion in 'Python' started by Gabriel Genellina, Aug 13, 2009.

  1. En Tue, 11 Aug 2009 23:22:27 -0300, Jus <> escribió:

    > Is there a way to have an Array of unsigned longlong (C Type: unsigned
    > longlong, Minimum size if bytes: 8) ?
    >
    > What I want to do is to store an array of bytes in an array of longlongs.
    >
    > To workaround this issue, I have tried to use the double array (TypeCode
    > 'd'), since the size in bytes is the same (8 for instance).


    Do you want to operate on it from inside Python, or manage it in some way?
    If not, use whatever format you like, even bytes. It's just a contiguous
    memory block with a known size.

    > But it gives me strange result. Please look at the example:
    >
    >
    > import array
    >
    > myByteArray = array.array('B', [1,2,3,4,5,6,7,8])
    > myLongArray = array.array('L', myByteArray.tostring() )
    >>> myLongArray = array('L', [67305985L, 134678021L])

    > myDoubleArray = array.array('d', myByteArray.tostring() )
    >>> myDoubleArray = array('d', [5.447603722011605e-270])

    >
    > If we convert the byte array to an hex form, we will get for the long
    > array:
    > [04030201, 08070605].


    What do you mean "convert the byte array to an hex form"?

    > If we convert this hex array to decimal, we then get the Long Array:
    > [67305985, 134678021].
    >
    >
    > Now, if we do the same exercise for the double array, the hex form will
    > look
    > like: [0807060504030201].
    >
    > And the decimal value would be: [5.784376957523072e+17].


    How did you got that?

    > Where does the 5.447603722011605e-270 value comes from ?


    The struct module agrees:

    py> s=struct.pack('q', 0x0807060504030201)
    py> struct.unpack('d', s)
    (5.447603722011605e-270,)

    And looking at the hex representation:

    py> (5.447603722011605e-270).hex()
    '0x1.7060504030201p-895'

    The 1. is implied, 7060504030201 are visible (52 bits), and the exponent
    is:

    py> hex(-895+1023)
    '0x80'

    So that value seems OK to me, at least with IEEE 754 hardware...

    --
    Gabriel Genellina
    Gabriel Genellina, Aug 13, 2009
    #1
    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. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    565
    Jack Klein
    May 9, 2005
  2. shaji
    Replies:
    10
    Views:
    446
    Martin Ambuhl
    Mar 22, 2006
  3. Ioannis Vranos
    Replies:
    11
    Views:
    738
    Ioannis Vranos
    Mar 28, 2008
  4. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    597
    Ben Bacarisse
    Mar 29, 2008
  5. Alex Vinokur
    Replies:
    9
    Views:
    764
    James Kanze
    Oct 13, 2008
Loading...

Share This Page