float to char conversion

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

float to char conversion

2,660 次查看
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.

标签 (1)
标记 (1)
0 项奖励
2 回复数

314 次查看
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 项奖励

314 次查看
fongguay
Contributor I
This worked, thank you!
0 项奖励