ASCII - FLOAT conversion

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 Hola,

   

 

   

I am having a problem with using the library function "atof". The compiler user guide describes the function as one which takes a pointer to a NULL terminated string as the argument and returns a double value.

   

 

   

Now what I have done is this. I have a program that creates a formatted string when user inputs each character of the string one by one by incrementing an initial character pointer (say *strptr) and finally giving a carriage return which is written as '\0' (NULL) in the last location of a string. The intial value of the character pointer is stored in another pointer variable.(say *ptrval, as in ptrval=strptr when program is initiated).

   

 

   

now to convert the ascii charater string that has been created in the previous sequence into a double value I use the function atof( ). eg. cval=atof(ptrval);

   

 

   

the variable 'cval' is monitored in the watch window. but it is not at all getting updated. please help me find out what could be wrong? is the way I have specified the argument correct? the compiler does not provide quality information on this.

   

 

   

Danke Amigos. 🙂

0 Likes
2 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

//----------------------------------------------------------------------------
// C main line
//----------------------------------------------------------------------------

   

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include <stdlib.h>

   

#define forever 1

   

double MyFloat;
char String[] = "4.123\0";

   

void main(void)
{
 while (forever)
 {
  MyFloat = atof(String);
  
  MyFloat = 0.0; // A breakpoint in this line shows that MyFloat has a Value of 4.123
 }

   

}
 

   

That works

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

@ Bob,

   

 

   

 Actually, I tried a similar code before & it worked.

   

 

   

But in this case, suppose we give k = UART_cReadChar() ; now upon a char input RX buffer contents are transferred to k and the Buffer is also cleared.  The UART_cReadChar() function empties the buffer everytime it is invoked, which I found out after many trials. Jes wanted to share what lead me to the right method.

   

 

   

This is for you - A screenshot showing atof() converted value in a watch window is attached.

   

Thanks - RAM

0 Likes