delphi - Something like StrTok() or Sscanf()? -


so, reading modbos on serial port , readings following : '+0020.8+0022.8-00.046-00.002-00.005-001.99+00.000+00.003';

basically, there 8 floating point readings, preceded plus or minus sign, although may of varying character length.

what's efficient way values array of float (or array of string or tsringlist)?

i not certain, might time critical, efficiency has way on elegance.

i this:

type   tfloatarray = array[0..7] of double;  procedure parsefloats(const afloatstr: string;   var afloatarray: tfloatarray); var   lpos: integer;   lnextpos: integer;   lpospositive: integer;   lposnegative: integer;   i: integer;   lformatsettings: tformatsettings; begin   //do not forget formatsettings, or problems regional settings   lformatsettings.decimalseparator := '.';   lformatsettings.thousandseparator := ',';   lpos := 1;   := 0 high(afloatarray)   begin     lpospositive := posex('+', afloatstr, lpos + 1);     lposnegative := posex('-', afloatstr, lpos + 1);     if lpospositive = 0       lnextpos := lposnegative     else if lposnegative = 0       lnextpos := lpospositive     else       lnextpos := min(lpospositive, lposnegative);     if lnextpos = 0       lnextpos := length(afloatstr) + 1;     afloatarray[i] := strtofloat(copy(afloatstr, lpos, lnextpos - lpos), lformatsettings);     lpos := lnextpos;   end; end;  //call var   lfloats: tfloatarray; begin   parsefloats('+0020.8+0022.8-00.046-00.002-00.005-001.99+00.000+00.003', lfloats); end; 

because there 8 float values, fixed array of 8 doubles enough. kept string manipulation minimum, once per floating point value string copied. important tformatsettings, otherwise errors on systems decimal separator not dot (like mine).

there no exception handling here, expect string 8 floating point values, nothing more, nothing less.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -