ascii to floating point
Richard S.
Posts: 70
I need a few thoughts as to how to convert signed ASCII values (-123.456 or +987.6543) to floating point.· Attached is a code snippet of Mike Green (with modification to allow up to 9 ASCII characters to be analyzed) that will convert unsigned ASCII to floating point.·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Richard in Michigan
Post Edited (Richard S.) : 11/15/2007 4:33:28 AM GMT
''================================================================================================= '' ASCII TO FLOATING POINT FORMAT ''================================================================================================= '' '' modified Mike Green code snippet to process 9 ascii characters ' ' CON ' _clkmode = xtal1 + pll16x ' use crystal x 16 _xinfreq = 5_000_000 ' 5 MHz crystal (sys clock = 80 MHz) ' ' VAR ' ' OBJ ' fm : "floatmath" fs : "floatstring" ' ' PUB ConvertStr(address) | c, d, dp ' d := false ' true if a decimal point seen dp := 0 ' number of places after decimal ' repeat while c := byte[noparse][[/noparse]address++] ' do while true (zero terminated string) if c == "." ' decimal point? mark as seen d := true ' duplicates will be ignored else result := result * 10 + (c - "0") ' accumulate integer value if d ' if decimal, count decimal places dp := dp + 1 ' no checking for more than 9 ASCII characters ' result := fm.fdiv(fm.ffloat(result),divisors[noparse][[/noparse]dp]) ' ' DAT ' divisors long 1.0, 10.0, 100.0, 1_000.0, 10_000.0, 100_000.0, 1_000_000.0 long 10_000_000.0, 100_000_000.0, 1_000_000_000.0 '
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Richard in Michigan
Post Edited (Richard S.) : 11/15/2007 4:33:28 AM GMT
Comments
1) Have a variable (say "positive") that is set to true initially
2) If the next character is "+", ignore it.
3) If the next character is "-", set "positive" to false and otherwise ignore the minus sign
4) Call ConvertStr with the rest of the string
5) If "positive" is false, call fm.fneg on the result from ConvertStr.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Richard in Michigan
=======================
This object accepts KEYBOARD ENTRY of a decimal/integer string.· Input characters display as ASCII / HEX.· BACKSPACE is allowed.· The DOT in the KEYBOARD ENTRY WINDOW is the next character position.· Once the completed number is entered, press the ENTER key and the number will be displayed as an ASCII STRING and· as a FLOATING POINT STRING (converted from f/p to string using the 'FLOAT TO STRING' object).· Pressing ESC key will reset the system, allowing for a·fresh restart.
Only 9 characters may be entered, which includes + and - signs, and decimal point.· Numbers larger than 999.99995 will produce a conversion error.· The float to string display format is set for a total of 9 characters wide with 4 decimal points.· Characters include numbers,·+ and - signs, and decimal point.
In VGA_TEXT.SPIN DAT section, change color pallete entry #1 to the same values as #0.
Other objects used in the program are:· FLOATMATH.SPIN, FLOATSTRING.SPIN, KEYBOARD.SPIN and VGA_TEXT.SPIN.
======================
note: updated file with a few cosmetic changes.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Richard in Michigan
Post Edited (Richard S.) : 11/18/2007 3:02:34 PM GMT