Shop OBEX P1 Docs P2 Docs Learn Events
copy byte array to long array — Parallax Forums

copy byte array to long array

pgbpsupgbpsu Posts: 460
edited 2010-05-03 17:00 in Propeller 1
I have a byte-wise array in HUB memory that I'd like to copy over to a long-wise array. I've been reading the Prop Manual (pg 209 in the printed version) and I'm still unsure how how to correctly copy all the data from one location to the other. As bytes of data become available from my input, I put them into byteArray. At some point I want to copy everything in byteArray over to longArray.

VAR
  byte byteArray[noparse][[/noparse]10*4]
  long longArray[noparse][[/noparse]10]

PUB
' copy from one location to the other using bytemove
bytemove(@longArray[noparse][[/noparse]0],@byteArray[noparse][[/noparse]0],4*10)  ' move all the bytes using bytemove

'OR
longmove(@longArray[noparse][[/noparse]0],@byteArray[noparse][[/noparse]0],10)  ' move all longs using longmove


'However, looking at pg 209 and 168 makes me think I SHOULD use something like this:

bytemove(@longArray.byte,@byteArray[noparse][[/noparse]0],4*10)




Are any of these correct? If not, how does one move bytes from a byte array to a long array?

Seems like I should have run into this before or see a post on this, but I haven't and my searches didn't turn up anything (had to use parallax search because my parallax google search no longer works).

Thanks,
Peter

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-05-03 16:39
    If you use bytemove then byte elements 0..3 in your byte array will end up in long element 0 of your long array. Is this what you want?

    If in fact you want byte 0 to end up in long 0, byte 1 to end up in long 1, etc., you're going to have to use a loop.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • pgbpsupgbpsu Posts: 460
    edited 2010-05-03 16:42
    Yes. If byteArray looks like this:
    byteArray[noparse][[/noparse]0] = 0x01
    byteArray = 0x23
    byteArray = 0x45
    byteArray = 0x67

    I'd like longArray[noparse][[/noparse]0] = 0x0123_4567
  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-03 16:57
    BYTEMOVE won't do what you want. The Propeller is "little-endian" with the least significant byte of a word or long value coming first in memory. Your byte array is in reverse order for this. You'll have to use a loop to copy this properly like:
    repeat i from 0 to lastLong
       j := i << 2
       longArray[noparse][[/noparse] i ] := byteArray[noparse][[/noparse] j ] << 24 | byteArray[noparse][[/noparse] j+1 ] << 16 | byteArray[noparse][[/noparse] j+2 ] << 8 | byteArray[noparse][[/noparse] j+3 ]
    
  • pgbpsupgbpsu Posts: 460
    edited 2010-05-03 17:00
    I think I've got this sorted. Thanks all.

    p
Sign In or Register to comment.