float to char conversion

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

float to char conversion

2,844件の閲覧回数
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 返答(返信)

498件の閲覧回数
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 件の賞賛
返信

498件の閲覧回数
fongguay
Contributor I
This worked, thank you!
0 件の賞賛
返信