Tokenizing a string without separators. How to parse a string in C?
Posted on 2006-11-20
I am writing a parser to handle an ASCII string representing voltages. The intent is to have this string separated into seven tokens. The string format is:
The # indicates to my program to select a "hex" case. The <cr> at the end signifies a carraige return which should be treated as an "end of line" flag. This last <CR> completes the string.
I want the string tokenized as follows:
# FF FF FF 0 FF 0 <CR>
There are no separators, only the position within the string is indicative of each token. Remember that these are being sent as ASCII characters over a serial line and I'll need to convert them from ASCII to integers (subtract 48 from the byte.)
The goal is to be able to perform calculations on the individual tokens. I am going to use the # as a start flag (not stored in a variable) and the <CR> as an end flag.
I realize this is a pretty basic issue for most programmers, but to me it's a hobby. I'm trying to build a microcontroller-driven servo and send it commands with my PC. I'm going to keep plugging away at the solution myself, but hope you can help. I would like to get an answer as soon as possible.