Shop OBEX P1 Docs P2 Docs Learn Events
Setting a byte array of 4 to the value of a long — Parallax Forums

Setting a byte array of 4 to the value of a long

Zap-oZap-o Posts: 452
edited 2011-01-14 09:15 in Propeller 1
I don't know if this is possible to do but, I wish to set a byte array value to a variable that is a long.
var
  byte rxdata [ 4 ] 
  long Qualifier

Pub main
  Bytemove(@rxdata,@Qualifier,4) 

Any ideas?

Comments

  • Heater.Heater. Posts: 21,230
    edited 2011-01-14 06:55
    How about:
    LONG(@rxdata) := Qualifier

    Of course that assumes that the bytes come out in the right order for your application. If not you have more work to do.
  • Zap-oZap-o Posts: 452
    edited 2011-01-14 07:08
    Okay that works but my bytes are backwards. As in rxdata [ 0 ] should be in the place of rxdata [ 3 ] and vise versa rxdata [ 3 ] should be in the place of rxdata [ 0 ]. Is there a cool byte flip operator?

    *edited - solved


    Thanks a million
  • SapiehaSapieha Posts: 2,964
    edited 2011-01-14 07:17
    Hi Zap-o.

    Sorry As I say it -- It is for have nice help for other Beginners.

    If You else other on write "SOLVED" ---> write even HOW it is solved even if that code are not NICE ---> it give other Beginners possibility how them need handle IT.




    Zap-o wrote: »
    Okay that works but my bytes are backwards. As in rxdata [ 0 ] should be in the place of rxdata [ 3 ] and vise versa rxdata [ 3 ] should be in the place of rxdata [ 0 ]. Is there a cool byte flip operator?

    *edited - solved


    Thanks a million
  • Zap-oZap-o Posts: 452
    edited 2011-01-14 07:29
    Yes, sorry:

    I solved this problem by simply using the command that Heater mentioned. And as my 2nd post mentioned another issue was created because my bytes were backwards in the rxdata array.

    original
    var
      byte rxdata [ 4 ] 
      long Qualifier
    
    Pub main
      Bytemove(@rxdata,@Qualifier,4)
    
    What I did not mention was that Qualifier := $48454C50 'HELP

    Solution
    var
      byte rxdata [ 4 ] 
      long Qualifier
    
    Pub main
     Long[@rxdata] := Qualifier
    

    And I changed Qualifier := 504C4548 'PLEH
  • SapiehaSapieha Posts: 2,964
    edited 2011-01-14 07:33
    Hi Zap-o.

    THANKS for understanding.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2011-01-14 07:33
    Define your variables in the DAT section rather than the VAR section for this.

    Example:
    DAT
    
    Qualifier     long
    rxdata        byte 0,0,0,0
    

    This way there isn't any conversion overhead. 'rxdata's BYTEs are aliased into 'Qualifier's LONG and vise versa.
    This method also uses half the nember of LONG variables
  • Zap-oZap-o Posts: 452
    edited 2011-01-14 07:49
    Well prop code as you know is brought from EEprom into ram. However I make sure to leave some space in EEprom (address above $8000+) so that I can store data into as a make shift hard drive without having to write new firmware. The Qualifier variable is one of these values that I need to write to EEprom and store. That said DAT wont work in my situation; reason why I use the VAR. :)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-14 08:49
    In general, the following is not a good way to do this:
     Long[@rxdata] := Qualifier
    

    Why? Because it assumes that rxdata[0] is long-aligned, which may not always be the case. And, if it's not, the assignment will not work the way you want it to.

    -Phil
  • ericballericball Posts: 774
    edited 2011-01-14 09:15
     Long[@rxdata] := Qualifier
    
    Why? Because it assumes that rxdata[0] is long-aligned, which may not always be the case. And, if it's not, the assignment will not work the way you want it to.
    Good point, thus Bytemove(@rxdata,@Qualifier,4) would be the recommended method of copying a long into a series of bytes. However, if an endian swap is required then the following would work:
      rxdata[0] := byte[@Qualifier][3]
      rxdata[1] := byte[@Qualifier][2]
      rxdata[2] := byte[@Qualifier][1]
      rxdata[3] := byte[@Qualifier][0]
    
Sign In or Register to comment.