ARM Technical Support Knowledge Articles

BYTE-WISE ACCESS TO FLOAT VALUES

Applies to: General Topics

Answer


Information in this article applies to:


QUESTION

I must store floating point values into an EEPROM space that is byte-wise accessed via I2C BUS. How can I split a float value into individual bytes?

ANSWER

One approach is to use a union that stores the float value along with a unsigned char array. For example:

union v  {
  float         f;
  unsigned char uc[4];
};

union v val;
float f;

extern void store_byte (unsigned char v);

void test (void)  {
  val.f = 34.567;           // set floating point value
  store_byte (val.uc[0]);   // write access to the bytes of the float value
  store_byte (val.uc[1]);
  store_byte (val.uc[2]);
  store_byte (val.uc[3]);

  val.uc[0] = 0x3F;         // set float value (1.1)
  val.uc[1] = 0x8C;
  val.uc[2] = 0xCC;
  val.uc[3] = 0xCD;
  f = val.f;                // read the value as FLOAT type.
}

MORE INFORMATION

SEE ALSO

Article last edited on: 2005-07-15 10:02:07

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential