How to format data for I2C

Hi,

I have about 10 values to transfer via I2C. What is the best way to format and transfer it?
4 of these values are Temperatures ( for example 120.3, float). Since the I2C library is just transfering bytes, how do I "convert" them? multiplicating with 10 doesnt work..

3 of the values are values like 1 and 0, no problem.. (uint8_t)

3 others are values from 0 to 6000 ( uint16_t)

The uint16_t and float values are requested in an intervall of 5-7hz, the uint8_t values ~20hz...

So I will do something like this since the "onRequest" funktion doesnt allow to choose what to request

#include <Wire.h>

uint8_t values[5] = {12, 45, 87, 34, 98};

void setup()
{
  Wire.begin(2);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}
void loop() {}

void requestEvent() {
  Wire.write("hello ");
}

void receiveEvent(int howMany) {
  uint8_t c;
  c = Wire.read();
  datarequested(c);
}

void datarequested(uint8_t whichbyte) {
  Wire.write(values[whichbyte]);
}

But how to transfer values like 123.4 or 6000 ?

Basically what you are asking is how do you send a multi-byte value through an interface that only sends one byte at a time. Easy way to do it is by leverage unions:

typedef union {
  float f;
  uint8_t b[sizeof(float)];
} FLOATI2C_t;

Use the FLOATI2C_t type instead of float:

FLOATI2C_t myFloat;

Assign it a value by accessing its float member:

myFloat.f = 123.0;

Send it using it's byte array member:

Wire.write(myFloat.b, sizeof(float));

Same concept for int.

I2c anything, http://gammon.com.au/Arduino/I2C_Anything.zip
Examples can be found at Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino with loads of other useful i2c info. Really helped me out.

You know what is stored in the memory when you have the value "120.3, float"? Take a look to this:

About the real question, the suggestion of Arrch, seams good to me. In other systems (not Arduino) I begin doing the same that Arrch says, but I end doing something like:

float myFloat;
myFloat = 120.3;
char* pChar;
pChar = (char*) &myFloat;
Wire.write(*pChar, sizeof(float));

These methods of transferring data all work the same way and will
work as long as both ends store the data the same way and in the same byte order.
If you are transferring between different processor types, or processors that have different
register sizes, it isn't this simple and you have to take extra steps.
i.e. this method of transferring the data is not fully portable across
all platforms.

--- bill

Im transfering data to a Raspberry, if it changes anything...
Ill have a look into the given examples, Thanks !