Shop OBEX P1 Docs P2 Docs Learn Events
How do you convert a byte[32] array to a long? — Parallax Forums

How do you convert a byte[32] array to a long?

mre_robmre_rob Posts: 31
edited 2007-05-17 17:03 in Propeller 1
If I have this:

var
byte tmp[noparse][[/noparse]32]
long dest

How do I get the byte array moved to the long correctly?


TIA...

Comments

  • BaggersBaggers Posts: 3,019
    edited 2007-05-16 15:22
    it would be easier for people to help you, if you were a little more descriptive on what dest is and what's in tmp and how you want it converting?

    as at the moment dest is 1 long ( 4 bytes ) so therefore tmp ( being 32 bytes ) could not be squeezed into it, unless dest was a pointer to a buffer for 8 longs.

    or, you could mean, there's an ascii string number stored in tmp, that you want converting to a long? ie "1234567890" you want it converting into one long 1234567890

    Baggers.
  • mre_robmre_rob Posts: 31
    edited 2007-05-16 15:27
    Baggers said...
    it would be easier for people to help you, if you were a little more descriptive on what dest is and what's in tmp and how you want it converting?

    as at the moment dest is 1 long ( 4 bytes ) so therefore tmp ( being 32 bytes ) could not be squeezed into it, unless dest was a pointer to a buffer for 8 longs.

    or, you could mean, there's an ascii string number stored in tmp, that you want converting to a long? ie "1234567890" you want it converting into one long 1234567890

    Baggers.
    AHH!!!

    Now it makes sense....

    There is an ascii string stored in tmp and I want to move it to a long. It appears that my size is off, but what would be the syntax to accomplish this type of memory move?

    Thx...
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-16 15:48
    If you want to simply copy the bytes from tmp to dest, use BYTEMOVE, like BYTEMOVE(@dest,@tmp,4) in order to move the first 4 bytes of tmp to dest. Read the descriptions in the Propeller manual for BYTEMOVE, WORDMOVE, LONGMOVE.
  • mre_robmre_rob Posts: 31
    edited 2007-05-16 15:51
    Mike Green said...
    If you want to simply copy the bytes from tmp to dest, use BYTEMOVE, like BYTEMOVE(@dest,@tmp,4) in order to move the first 4 bytes of tmp to dest. Read the descriptions in the Propeller manual for BYTEMOVE, WORDMOVE, LONGMOVE.
    Thanks Mike, I will..
  • M. K. BorriM. K. Borri Posts: 279
    edited 2007-05-16 16:43
    I think he wants to move the string "123456" into a 4-byte binary variable that has the value 123456.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-16 17:02
    If that's so, then the following simple string to integer conversion will work.
    The string has to be terminated with a zero byte and there's no error checking.
    PRI convert(ptr) | digit
       repeat while digit := byte[noparse][[/noparse]ptr++]
          result := result * 10 + digit - "0"
    
    


    You'd call "dest := convert(@tmp)"
  • M. K. BorriM. K. Borri Posts: 279
    edited 2007-05-16 18:21
    PRI asctoint(ptr) | digit
       repeat while digit := byte[noparse][[/noparse]ptr++]
          if digit > constant("0"-1) and digit < constant("9"+1)  
              result := result * 10 + digit - "0"
          else return
    
    



    ' Minimal error checking -- this will work for NMEA strings, more or less, but you will end up taking in only the integer part of a value. If you want, get the navcom ai source (see my sig) and use the ParseNextFloat routine that is in FtoF.spin -- does the same thing, only generates a floating point result and has robust error checking.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
  • mre_robmre_rob Posts: 31
    edited 2007-05-17 10:56
    M. K. Borri said...
    PRI asctoint(ptr) | digit
       repeat while digit := byte[noparse][[/noparse]ptr++]
          if digit > constant("0"-1) and digit < constant("9"+1)  
              result := result * 10 + digit - "0"
          else return
    
    



    ' Minimal error checking -- this will work for NMEA strings, more or less, but you will end up taking in only the integer part of a value. If you want, get the navcom ai source (see my sig) and use the ParseNextFloat routine that is in FtoF.spin -- does the same thing, only generates a floating point result and has robust error checking.

    so using your ParseNextFloat would conver the Ascii string to a "real" float and I would store that into my long value for calcs later...

    So If I am parsing Lat, then the code would be similar to this to get the correct float

    Please assume that "f" is the ftof object and "tmp" has the ascii string to convert

    f.ParseNextFloat(@tmp,long[noparse][[/noparse]constant(objMem#SensorDataAddress + objMem#GprmcLat)])

    Look like I am on the right track?

    TIA..

    Rob.
  • M. K. BorriM. K. Borri Posts: 279
    edited 2007-05-17 14:40
    Yep, pretty much [noparse]:)[/noparse]

    ParseNextInt/ParseNextFloat is optimized to use in GPS strings, because it eats up the digits in the original string (replaces them with ####'s) and returns where to
    start looking for the next number if you're in a hurry.

    So for example, if you have a NMEA string that has (comma separated) values for lat, lon and heading, you can do


    f.ParseNextFloat(@tmp,long[noparse][[/noparse]constant(objMem#SensorDataAddress + objMem#GprmcLat)])
    f.ParseNextFloat(@tmp,long[noparse][[/noparse]constant(objMem#SensorDataAddress + objMem#GprmcLon)])
    f.ParseNextFloat(@tmp,long[noparse][[/noparse]constant(objMem#SensorDataAddress + objMem#GprmcHeading)])


    and it will work. I did this because, obviously, it was supposed to work with nmea or nmea-like strings in the first place. Nifty, no?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
  • mre_robmre_rob Posts: 31
    edited 2007-05-17 14:47
    Awesome... I'll get this thing running yet.... again, I appreciate your help.
  • M. K. BorriM. K. Borri Posts: 279
    edited 2007-05-17 17:03
    If you downloaded my stuff, see how I do it (look in SensorParser and ExpressionParser, not GPSParser because the gps parser right now uses the Garmin format) and feel free to copy [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
Sign In or Register to comment.