Need help with data parsing
turbosupra
Posts: 1,088
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
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
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?
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).
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.
Granted I've missed quite a few memos over the years.
Paul
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).
(sorry for the thread hijack)
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
... where n is the byte number, 0 to 3 for longs, 0 or 1 for words.
If 242085316 is a number you can extract portions with simple math