Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

converting from float to string

Status
Not open for further replies.

erodeboy

Member level 5
Joined
Nov 15, 2005
Messages
80
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
India
Activity points
1,987
I want to convert a floating point value into a string using c. am using keil help me with the code.

thanks in advance
 

sprintf() ?
 

    erodeboy

    Points: 2
    Helpful Answer Positive Rating
I would need to separate the mantissa from the fraction and convert every digit to an ASCII putting the point "." symbol where needed
 

The atof function converts string into a floating-point value. simillarly i want a function to convert a float into a string so that i can send it to lcd.
 

As artem said, look at sprintf();


Code:
char buffer[24];
float x = 1.5;

sprintf(buffer, "Float to string %f", x);
 

    erodeboy

    Points: 2
    Helpful Answer Positive Rating
"sprintf" consumes lot of memory space is there anyother way out....
 

This routine prints floating point in 1 decimal place w/o using printf /sprintf.

Code:
/*
*********************************************************************************************************
*                        CONVERT A FLOATING POINT NUMBER TO STRING WITH 1 DECIMAL PLACE
*
* Description :	This function converts a floating point to a null terminated string 
*				with 1 decimal place.
*			
*				Examples:
*					
*				float f = 9.567;
*				ftoa(&s[0], f);		//s[]={'9','.','5', 0}
*				float f = -0.189;
*				ftoa(&s[0], f);  	//s[]={'-', '0', '.', '1', 0}
* Arguments   : 'unsigned char* buf' 	is the pointer to the string holding the conversion result
*				'float f'				is the input floating point
* Returns     : Returns the string with unsigned char* buf pointing to.
* Notes		  : This routine modified from itoa10() in ..\sample\misc folder of ht-picc
*				If more decimal places required, modify the last section of this code
*				Range of f in (-3,276.7, 3,276.7)
*				This function does print result like 0.0.
*********************************************************************************************************
*/
void ftoa(unsigned char *buf, float f) {
	unsigned int rem;
	unsigned char *s,length=0;
	int i;

	i = (int)((float)f*10);

	s = buf;
	if (i == 0){ 		//print 0.0 with null termination here
		*s++ = '0';
		*s++ = '.';
		*s++ = '0'; 
		*s=0; 			//null terminate the string
	} else {	
		if (i < 0) {
			*buf++ = '-';
			s = buf;
			i = -i;
		}
		//while-loop to "decode" the long integer to ASCII by append '0', string in reverse manner
		//If it is an integer of 124 -> string = {'4', '2', '1'}
		while (i) {
			++length;
			rem = i % 10;
			*s++ = rem + '0';
			i /= 10;
		}
		//reverse the string in this for-loop, string became {'1', '2', '4'} after this for-loop
		for(rem=0; ((unsigned char)rem)<length/2; rem++) {
			*(buf+length) = *(buf+((unsigned char)rem));
			*(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1));
			*(buf+(length-((unsigned char)rem)-1)) = *(buf+length);
		}

		/* Take care of the special case of 0.x if length ==1*/	
		if(length==1) {
			*(buf+2) = *buf;
			*buf = '0';
			*(buf+1) = '.';
			*(s+2)=0; 			//null terminate
		} else {
			*(buf+length) = *(buf+length-1);
			*(buf+length-1)='.';
			*(s+1)=0; 			//null terminate
		}
	}
}

John Leung
www.TechToys.com.hk
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top