Shop OBEX P1 Docs P2 Docs Learn Events
Need help with data parsing — Parallax Forums

Need help with data parsing

turbosupraturbosupra Posts: 1,088
edited 2014-12-19 10:48 in Propeller 1
I'm trying to pass a long to a function and have it return the bytes. I thought this would be easy, but it has proven not to be. In a high level language I can set the return type when I declare a function, but I have no idea how to return an array in spin?

I've spent the past day trying to figure this out and seen a bunch of examples that were similar, but I cannot get any of them to work, they either display nonsense or the wrong thing?

Thanks for reading

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-12-19 05:14
    You mean something like this?
    VAR
      byte  r[4]
    
    PUB main
    
      split(cnt, @r{0})
    
    PRI split(value, storage) : n
    
      repeat 4
        byte[storage++] := value.byte[n++]
    
    Note, you can't simply return a reference to local variables as they stop existing after returning. For this reason a reference to an array which is/remains valid in the caller's context is used.
  • turbosupraturbosupra Posts: 1,088
    edited 2014-12-19 05:28
    Hi Kuroneko,

    I tried to implement that into my code without success. The local variable lesson makes sense, I will have to remember that.

    I've attached my project with your code in it, what am I doing wrong?
    241978260
    196
    237
    109
    14
  • kuronekokuroneko Posts: 3,623
    edited 2014-12-19 05:33
    turbosupra wrote: »
    I tried to implement that into my code without success.
    How do you define success? I admit it's untested code (at work right now) but all you get from it is a byte split system counter (I could have used split(exampleValue, @r{0}) instead).

    From your example I can see that the value to be split was 242085316 (14 << 24 | 109 << 16 | 237 << 8 | 196) which makes sense since cnt has advanced from its original value (241978260).
  • turbosupraturbosupra Posts: 1,088
    edited 2014-12-19 05:44
    I believe your code works, I came across a similar example in my searching that you provided to someone else (in 2012 I think).

    Success is my ability to implement it and understand it, of which I haven't been successful at yet. I thought I had implemented it wrong, but based on your response I implemented it right and just don't understand. I'll keep working at it.
  • PaulPaul Posts: 263
    edited 2014-12-19 05:48
    [serious] I'm looking at that @r{0} and scratching my head. Is this a thing now? [/serious]
    Granted I've missed quite a few memos over the years.

    Paul
  • turbosupraturbosupra Posts: 1,088
    edited 2014-12-19 05:50
    I have forgotten what that means. I've also forgotten what the : in a function is for? Maybe the same thing as a | ?
    Paul wrote: »
    [serious] I'm looking at that @r{0} and scratching my head. Is this a thing now? [/serious]
    Granted I've missed quite a few memos over the years.

    Paul
  • kuronekokuroneko Posts: 3,623
    edited 2014-12-19 05:53
    Paul wrote: »
    [serious] I'm looking at that @r{0} and scratching my head. Is this a thing now? [/serious]

    The intention is to pass the address of the first element of the byte array named r. Since the first index is 0 that would be @r[0]. For index 0 the actual index is optional (in SPIN array[0] is the same as array but the latter is smaller). To remind me that it's still an array I use a comment which looks like an indexed access modifier, i.e. @r{0}.

    The colon after a function name separates the return parameter (by default result but it can be aliased, n in my example). It is also the only local variable which is cleared (important for the loop).
  • PaulPaul Posts: 263
    edited 2014-12-19 06:06
    Okay, it makes sense as far as documenting what you are doing for your future self and others to understand "r" is an array. Very clever.

    (sorry for the thread hijack)
  • turbosupraturbosupra Posts: 1,088
    edited 2014-12-19 06:09
    Thank you for that format explanation Kuroneko.

    I also figured out how the function works and why it works that way. It works with the value I would like to send to it. If I wanted to split the value literally, is there an easy way to do that? For example if I wanted to split 242085316 into 2420 and 85316, is a string parsing function needed? I was trying to use a string parsing function originally.




    split(242085316, @r{0})


    ' 242085316 in binary is 1110 01101101 11101101 11000100
    11000100
    11101101
    01101101
    00001110

    196
    237
    109
    14
  • kuronekokuroneko Posts: 3,623
    edited 2014-12-19 06:57
    turbosupra wrote: »
    If I wanted to split the value literally, is there an easy way to do that? For example if I wanted to split 242085316 into 2420 and 85316, is a string parsing function needed?
    You could use string manipulation for this but you could also divide by 100000 to get the top part (2420) and variable // 100000 to get the lower part (85316).
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-12-19 07:10
    In Spin you have direct access to the bytes in a word or long -- like this
    myByteVar := someLongVar.byte[n]
    


    ... where n is the byte number, 0 to 3 for longs, 0 or 1 for words.

    I wanted to split 242085316 into 2420 and 85316


    If 242085316 is a number you can extract portions with simple math
    hi := longVar / 100_000
      lo := longVar // 100_000
    
  • turbosupraturbosupra Posts: 1,088
    edited 2014-12-19 10:48
    Ok, thank you gentlemen
Sign In or Register to comment.