Get data between delimiters starting from a certain point.
eagletalontim
Posts: 1,399
What I am trying to accomplish is a simple function that I can call to extract a decimal value from a string right after the "search value".
I have a byte array (BYTE buffer[255]) that can look like this : R:C:03:V:12: D:7231:# (Ignore space between ":" and "D") What I would like to do is call this new function by using Value := extract_data(@buffer, "C") Variable "Value" is a LONG. When parsed properly, the number 3 will be the new value. Same would go if using : Value := extract_data(@buffer, "D") which would return 7231 as the new value.
There is a string method I found on the forums and have tried to make it work, but no go Any help is greatly appreciated!
I have a byte array (BYTE buffer[255]) that can look like this : R:C:03:V:12: D:7231:# (Ignore space between ":" and "D") What I would like to do is call this new function by using Value := extract_data(@buffer, "C") Variable "Value" is a LONG. When parsed properly, the number 3 will be the new value. Same would go if using : Value := extract_data(@buffer, "D") which would return 7231 as the new value.
There is a string method I found on the forums and have tried to make it work, but no go Any help is greatly appreciated!
Comments
*edit, forgot delimiters*
*aedit*
This should be something like you want?
When I type in the PST input : R:C:43:G I am trying to get "43" to show up as Value.
Note * : n[?] showed up as an error.
You need to make the ? the max size of your string (the longest value you expect to return) + 1 (for zero delimiter) / 4 (bytes to long)
Now, I should have used num.FromStr
Other than that, I'd have to actually prototype it. I was just hoping to give a jumping off point. I think there are several errors at this point. I'll try to revisit it when I have a few extra minutes..
*edit*
I think this is closer,..
The first repeat loop (t) looks for the key. Increment t to point at first byte of value, then the second repeat loop looks for the delimiter. Then copy to a temporary variable, null terminate the string and pass it to numbers. (Max number string size would be 16 bytes in this example.
Additionally, JonnyMac provided some real good insight to parsing at this post http://forums.parallax.com/showthread.php/159947-HELP.spin-I-Am-Going-In-Circles-And-Getting-Dizzy-Byte-Array?p=1313935&viewfull=1#post1313935 and then expands his his discussion in Post #9 of that thread, and then further expansion of the same concept, is made in the thread of the first link provided.
You should now be well armed, with several methods
Bruce
I added an ELSE to the extract_data IF statement and apparently I am calling this function incorrectly because it always hits the ELSE....
Attempts at calling have been :
value1 := extract_data(@buffer, "C", ":")
value1 := extract_data(buffer, "C", ":")
value1 := extract_data(@buffer, string("C"), string(":"))
value1 := extract_data(buffer, string("C"), string(":"))
None have worked....
num.fromStr(@n, num#dec)
And it should increment t by 2...
*edit*
Fixed a couple mistakes
byte[@n] [i + 1] := 0
as
n.byte [i + 1] := 0
Trying to get used to using the second method since it seems cleaner to me. (usually)
Glad it finally worked. It could be optimized a bit depending on what you are doing. Returning false might be bad if you are expecting negative numbers... In that case, an abort would be best..
I realize that you have a single character and colon, but what if your input changes to something like "TEMP=123 FLOW=456"? My method will still work. The lesson here is to write code that solves the immediate problem, yet is open to others within the realm of possibility.
Anything with "R:C:" was send from the browser. The next line is the data from the Prop.
When I call command 10, the string looks like this : <R:C:10:N:6:V:74:#> I always get "Failed Saving!"
Here are bits and pieces of my receiving / processing code on the Prop :