CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to convert a float in two int8

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

How to convert a float in two int8
PostPosted: Thu Sep 10, 2009 11:45 am     Reply with quote

Hi, I have a result of reading a sensor expressed in float with 2 decimal (TEM), I want to convert this number float in two int8 to TX to a PC (buffer_tx[0] and buffer_tx[1]), how I can do it?

Code:
int buffer_tx[2];

printf("%f\r\n",TEMP);    // print 27.50

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 10, 2009 12:21 pm     Reply with quote

Look at the floor() function in the CCS manual. It shows an example
of how to get the fractional part of the floating point number.
Then with some math and the atoi() function, you can convert each part
into an 8-bit integer value.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Sep 10, 2009 2:37 pm     Reply with quote

Maybe you are looking for the make8() function? It is well documented in the manual once you know what it is called. A float is not going to fit in 2 bytes without some work.
_________________
The search for better is endless. Instead simply find very good and get the job done.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 10, 2009 3:19 pm     Reply with quote

pilar wants to take a floating point number in this format xx.yy, and
convert the xx and the yy into 8-bit integers, and transmit them as two
bytes. I didn't write a routine, I just pointed out the floor() function,
because I think pilar should try to do it first.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Thu Sep 10, 2009 9:39 pm     Reply with quote

Hi PCM programmer, thank you, I did not know this function.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group