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

Float into 4 int

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







Float into 4 int
PostPosted: Mon Oct 20, 2003 8:49 am     Reply with quote

Hi all.

do u guys have any idea on how to convert a float number (32bit)
into 4 int (8-bit) ?

i need it so i could convert my float number to send ove rto dallas one wire device. they recive one byte at a time... arghh.

pls help.. thank you.
Ttelmah
Guest







Re: Float into 4 int
PostPosted: Mon Oct 20, 2003 9:07 am     Reply with quote

AKA wrote:
Hi all.

do u guys have any idea on how to convert a float number (32bit)
into 4 int (8-bit) ?

i need it so i could convert my float number to send ove rto dallas one wire device. they recive one byte at a time... arghh.

pls help.. thank you.

Been answered many time before.
Two 'different' methods. One is just to use a pointer, cast it to point to an int8, and read the bytes using this. However my favourite (and it gives more efficient code :-), is to use a union. So:

Union {
int8 b[4];
float flt;
} value;

Then 'value.flt', is the floating point number, and can be used as normal in your code, while 'value.b[0]', now refers to the first byte of the number, through to 'value.b[3]', holding the last byte.
So:

int count;
value.flt=1000.0;
for (count=0;count<4;count++) {
sendbyte(value.b[count]);
}

Will (assuming you have a suitable 'sendbyte' function, that expects to receive an int8), send the individual bytes in order. In the example given, sendbyte will be called first with '0x88', then with '0x7A', then with 0, and again with 0.

Conveniently, the same trick, can be used at the other end, to 'reassemble' the bytes to form the float (just put the bytes into 'value.b[n]', and when all the bytes are in place, 'value.flt', will contain the original number).

You can even 'cheat' with this, since the CCS compiler, does not restrict implied casting of numbers. So:

union FTOB {
int8 b[4];
float flt;
};

void sendfloat(union FTOB value) {
int count;
value.flt=1000.0;
for (count=0;count<4;count++) {
sendbyte(value.b[count]);
}
}

Can be called as:

sendfloat(1000.0);

(or with a float variable), and will automatically be treated as the union inside the subroutine, allowing the bytes to be individually accessed.

Best Wishes
AKA
Guest







PostPosted: Mon Oct 20, 2003 9:16 am     Reply with quote

geezs thanks will try that
AKA
Guest







stupid ques
PostPosted: Mon Oct 20, 2003 9:58 am     Reply with quote

yo .,, thanks for the code.

got it implemented but ccs compiler says" Expecting a structure/union"
is it is already defined, as in

union FTOB {
int8 b[4];
float flt;
};

thanks .. again
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