Shop OBEX P1 Docs P2 Docs Learn Events
String-to-float example, please (strtofloat) — Parallax Forums

String-to-float example, please (strtofloat)

I have tried many ways to get the strtofloat method to run. I run it as a method in the cfloatstr.spin library from the OBEX. Nothing works, although I know I correctly pass the starting address for an array that holds ASCII characters for -12345. The array ends with a $0 value (zero value, not ASCII 0). I always get 0 as an answer. An example of how to use the strtofloat method with a real value would be appreciated. I don't see any dependencies in the code, but perhaps I need something else running to get the strtofloat method to work. Thanks. --Jon

Here are the important components in my test program (not complete program):

OBJ
pst : "Extended_FDSerial"
StoF : "cfloatstr" 'part of clib

repeat index from 0 to 5 'Prints sign and digits as a check
pst.tx(byte[@Data_Bytes][index])
pst.tx(13)

pst.str(string("Hex address for start of string: "))
pst.hex(@Data_Bytes, 8) 'Display address of start of string
pst.tx(13) 'New line

Float_Value := StoF.strtofloat(@Data_Bytes) 'Send string-to-float array address

pst.str(string("Floating-point as hex: "))
pst.hex(Float_Value, 8) 'Print float value as 8 hex digits
pst.tx(13)
pst.str(string("...finished..."))
pst.tx(13)


DAT
Data_Bytes BYTE "-", "1", "2", "3", "4", "5", $0

Comments

  • cfloatstr is written for use by C code and uses an extra level of indirection. .strtofloat expects to be given the address of a long which contains the address of the string. In your DAT section, you'll need:

    Data_Addr LONG 0

    In your code, you'll need:

    Data_Addr := @Data_Bytes
    Float_Value := StoF.strtofloat(@Data_Addr)

    If you want to use the equivalent routine in FloatString 1.2 from Michael Park, you won't need the additional indirection.
  • Many thanks, Mike. I appreciate your help. --Jon
Sign In or Register to comment.