Shop OBEX P1 Docs P2 Docs Learn Events
Byte arrays and math — Parallax Forums

Byte arrays and math

jokerswildjokerswild Posts: 31
edited 2007-12-06 21:54 in General Discussion
G'day all,

This is for an SX28.

I have an byte array with·3 fields

ie;
gpsknots·· VAR·· Byte(2)
speed······ VAR·· Byte

where;
· gpsknots(0) = 1
· gpsknots(1) = 4
· gpsknots(2) = 3

To put this in a single variable i can use in operations i came up with this;

· speed = 0
· tmpB1 = gpsknots(0) * 100
· tmpB2 = gpsknots(1) * 10
· tmpB3 = tmpB1 + tmpB2
· speed = gpsknots(2)
· speed = speed + tmpB2
· speed = speed + tmpB1·
I'm still playing around with the code and haven't tested anything yet but can someone tell me if this will work and if its efficient?

Cheers!

Comments

  • BeanBean Posts: 8,129
    edited 2007-12-06 02:34
    speed = gpsknots(0) * 10
    speed = speed + gpsknots(1)
    speed = speed * 10
    speed = speed + gpsknot(2)


    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-12-06 14:29
    Another approach is to create a function that takes the address of your array and the field width and converts it to a single value -- like this:

    ' Use: ARRAY2DEC address, digits
    
    FUNC ARRAY2DEC
      tmpB1 = __PARAM1                              ' address of array
      tmpB2 = __PARAM2                              ' field width
    
      tmpW1 = 0                                     ' clear result
      DO WHILE tmpB2 > 0
        tmpW1 = tmpW1 * 10                          ' shift digits left
        tmpB3 = __RAM(tmpB1)                        ' get digit
        tmpW1 = tmpW1 + tmpB3                       ' add to result
        INC tmpB1                                   ' point to next element
        DEC tmpB2
      LOOP
      RETURN tmpW1
      ENDFUNC
    

    Post Edited (JonnyMac) : 9/20/2008 12:56:44 AM GMT
  • jokerswildjokerswild Posts: 31
    edited 2007-12-06 21:54
    Awesome thanks for the ideas guys!
Sign In or Register to comment.