Parsing A string
bdickens
Posts: 110
I'm having to code well away from the hardware so if this is really basic, I apologize but I can't check it with the LCD right now.
I need to parse a string into 2 separate strings. The first 4 characters identify the data stream. Pretty sure the first datamove will do that. But the second, will this work, or do I need an @ in front of the InDataRec{4} ?
NOTE: Those are really square brackets after the InDataRec but I could not for the life of me figure out how to post.
Dat
inDataRec btye $FF, 50, "WR01THE REST "
First4 byte $FF, 4, " "
Lastpart byte $FF,46
Pub Start
Bytemove(First4,InDataRec,4)
Bytemove(LastPart,InDataRec{4},46)
Post Edited (bdickens) : 7/24/2010 7:06:52 PM GMT
I need to parse a string into 2 separate strings. The first 4 characters identify the data stream. Pretty sure the first datamove will do that. But the second, will this work, or do I need an @ in front of the InDataRec{4} ?
NOTE: Those are really square brackets after the InDataRec but I could not for the life of me figure out how to post.
Dat
inDataRec btye $FF, 50, "WR01THE REST "
First4 byte $FF, 4, " "
Lastpart byte $FF,46
Pub Start
Bytemove(First4,InDataRec,4)
Bytemove(LastPart,InDataRec{4},46)
Post Edited (bdickens) : 7/24/2010 7:06:52 PM GMT
Comments
2) It's hard to tell what you're doing. Please do not use cut-and-paste to include code in your message. Use the Attachment Manager that you get when you click on the Post Reply button. Alternatively, you can use the [noparse][[/noparse] code ] and [noparse][[/noparse] /code ] tags around your code. Note that other tags like [noparse][[/noparse] i ] will be stripped out by the forum software and will affect the formatting, not what you expect.
Note that BYTEMOVE expects addresses of the byte arrays used for strings. Normally you get the address of a string item by using @item.
The format of your BYTE statements doesn't make sense. Please look at the manual again. Are you trying to have multiple instances of the $FF byte values? If that's what you want, you have to put the count in square brackets after the value like: $FF[noparse][[/noparse] 4 ]
Post Edited (Mike Green) : 7/24/2010 7:31:58 PM GMT
I'll relook the data section.
You need to make the data section behind first4 and lastpart bigger. When you do a bytemove to first4 you will overwrite the lastpart section.
dat
first4 byte $ff, 4, 0[noparse][[/noparse]·5 ]
lastpart byte $ff, 56, 0[noparse][[/noparse]57]
the 0[noparse][[/noparse]·5 ]·will place 5 zeros in memory. Please note it's one more zero than needed for the 4 characters, so the last zero is the stringend character. This allows to use the SPIN string-functions. If you have your own methods which use the length given as the second byte, you can of course replace the 5 with a 4.
Then I suppose what you want to do is:
bytemove( @frist4+2, @inDataRec + 2, 4 )
bytemove( @lastpart+2, @inDataRec + 6, 56 )
the +2 skips the $ff, 4 and $ff , 56 as I think you want to keep this information.
Post Edited (MagIO2) : 7/24/2010 9:45:44 PM GMT
I have a string InDataRec which happens to be 30 bytes long
I have a string called First4
and another called LastPart
The first 4 bytes of InDataRec are a transaction header.
The rest are the transaction itself.
The routine is passed InDataRec. The contents of that string are "WR01TheStringThatCameIn1234568"
I need to separate this into two strings. In basic, I would have coded this as
First4 = substr(InDataRec,1,4)
LastPart = substr(InDataRec,5,Len(InDataRec))
And First4 would have been "WR01"
and LastPart would have been "TheStringThatCameIn1234568"
I agree, I did not do the DAT correctly and I appreciate all the input. So presuming I fix that to be 3 strings, how do I get this parsed.
Thanks
VAR byte InDataRec[noparse][[/noparse] 31 ], First4[noparse][[/noparse] 5 ], LastPart[noparse][[/noparse] 27 ]
The extra byte is for the zero byte that marks the end of any string
You'd then do:
BYTEMOVE( @First4, @InDataRec, 4) ' Copy the first 4 characters to First4. Note no checking is done.
First4[noparse][[/noparse] 4 ]~ ' Force the last byte to be zero (after the 4 characters)
and:
BYTEMOVE( @LastPart, @InDataRec+4, STRSIZE(@InDataRec+4)+1) ' Copy the rest of the string and the zero terminator
You could compare the prefix as follows:
IF STRCOMP( @First4, STRING("WR01"))
' Do something if equal
Post Edited (Mike Green) : 7/24/2010 10:20:38 PM GMT