float to char conversion

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

float to char conversion

2,641 Views
fongguay
Contributor I
I'm sure that this is a popular question:
How do you convert a 4 byte float into 4 chars?  I have tried type casting and doing funny stuff with pointers, but it appears that Code Warrior is saying "I know what you're trying to do, and I won't let you." (I either get an explicit or implicit change of data type error).  All I want is the raw float, in bits, in IEEE-754 format, broken into 4 bytes so that I can ship it out one byte at a time.  I haven't tried the sprintf deal yet, seems a bit impractical, but if that's what I have to do...

thanks in advance for any help here.

Labels (1)
Tags (1)
0 Kudos
2 Replies

295 Views
BlackNight
NXP Employee
NXP Employee
Hello,
I would use a union like this one:
union {
  float f;
  char c[4];
} u;


with u.f = 1.0  you store a float, and with u.c[0] etc you can get the indivdual bytes.
Hope this helps.

BlackNight
0 Kudos

295 Views
fongguay
Contributor I
This worked, thank you!
0 Kudos