Hi Amit,
You will need to study up a little more on the C programming language.
If a is of type float then the compiler will not allow a >> 8. A bitwise shift operator doesn't make sense for a float. This is why your first example did not work.
In your second try you first cast the float to an 8-bit integer and then shift it. This is legal but not what you want. If a is 1.125 the cast will convert this to the integer 1 and then shift the result--you are losing all your floating point information.
In the third example you are back to shifting a float.
A way to do this is to use a union. It tells the compiler that you'd like to interpret a piece of memory as different data types. In this case you have four bytes that you want to interpret as either a float or a 32-bit unsigned number.
void test(void)
{
uint8_t buffer[4];
union
{
float f;
uint32_t i;
} u;
u.f = 1.125;
buffer[0] = (uint8_t)(u.i>>24);
buffer[1] = (uint8_t)(u.i>>16);
buffer[2] = (uint8_t)(u.i>>8);
buffer[3] = (uint8_t)(u.i);
}
If in your buffer you are mixing types then you have other issues to deal with. How do you know how to decode your stream of bytes? If you have mixed data but it is the same each time I would define a struct that hold these and then write the structure out as raw binary. You'll need to understand how the data is packed which gets in to advanced territory but would look something like (does this editor have the ability to quote code???):
void test_2(void)
{
struct
{
float f;
uint32_t u32;
uint8_t b;
} frame;
frame.f = 1.125;
frame.u32 = 0xDEADDEED;
frame.b = 0xAB;
//write_to_device(&frame, sizeof(frame));
}
There are a lot of details to get right when doing stuff like this. You'll have to spend a lot of time working to understand it.