convert a float to a series of bytes

How can I convert a float variable to a series of bytes. The float is a longitude from the tinyGPS library

Below is code to convert an unsigned long integer into bytes. I think float would work too since it’s also 4 bytes

Convert 4 bytes to unsigned long (Source)

// convert from an unsigned long int to a 4-byte array
byteArray[0] = (int)((longInt >> 24) & 0xFF) ;
byteArray[1] = (int)((longInt >> 16) & 0xFF) ;
byteArray[2] = (int)((longInt >> 8) & 0XFF);
byteArray[3] = (int)((longInt & 0XFF));

An alternate solution would be to first apply the mask, and then shift:

byteArray[0] = (int)((longInt & 0xFF000000) >> 24 );
byteArray[1] = (int)((longInt & 0x00FF0000) >> 16 );
byteArray[2] = (int)((longInt & 0x0000FF00) >> 8 );
byteArray[3] = (int)((longInt & 0X000000FF));

Convert the 4-byte array back into an unsigned long int:

unsigned long int anotherLongInt;
anotherLongInt = ( ((unsigned long)byteArray[0] << 24)
                 + ((unsigned long)byteArray[1] << 16)
                 + ((unsigned long)byteArray[2] << 8)
                 + ((unsigned long)byteArray[3] ) );

And an alternate solution to accomplish the same:

anotherLongInt  = ((unsigned long) byteArray[0]) << 24;
anotherLongInt |= ((unsigned long) byteArray[1]) << 16;
anotherLongInt |= ((unsigned long) byteArray[2]) << 8;
anotherLongInt |= ((unsigned long) byteArray[3]

arduinoPi:
How can I convert a float variable to a series of bytes.

Why do you want to do this?

I am transmitting the latitude and longitude from the Arduino to an Xbee and from another Xbee to a computer. The computer program I am making can only read individual bytes so I am trying to send the latitude and longitude into a series of bytes.

arduinoPi:
How can I convert a float variable to a series of bytes. The float is a longitude from the tinyGPS library

Perhaps using a union? Just map the float variable on top of the byte array. Then your sketch can reference the value as either a float or an array of bytes as you wish.

Lefty

Is this from a GPS? The data comes from the GPS in a series of bytes. Why convert those to a float and muck around with unions and then convert them back? All serial comms is a series of bytes.

Look, if you want to send 147.234E to another PC, just do this:

Serial.println ("147.234E");

Series of bytes. Done.

"Series of bytes" is too fuzzy. Data is always stored as a series of bytes. You need to decide which format you want to use to send the bytes.

Do you want to use the binary format that the system uses to store the float in memory? If that's the case, you don't need to convert it at all. You just suck the data out of memory and send it. You can use an union if you want, or simply typecast it or use a pointer.

// untested code

byte seriesOfBytes[4];
float floatVariable;

*((float *)seriesOfBytes) = floatVariable;

Do you want to convert it to ASCII? It may be that the data was originally in ASCII format and instead of converting it to floats you can use a String or a character array. Or you can convert floats to ASCII on this end and convert it back to a float after the target computer receives it.

Do you want to convert it to ASCII Hexadecimal? That's easier in some ways and harder in others. The advantage is that it's very easy to convert and easily understandable from a human standpoint.