I'm designing a probe balloon for a drone using the Raspberry Pi 3B+ and I need to transmit a lot of information through a limited communication that is restricted to messages within just 12 bytes. My information mainly comprises all the values of the parameters surrounding it like altitude, temperature, longitude, latitude, average acceleration, etc.

Now, I'm building my datagram where I wanted to do a 32-bit lossy conversion for all the longitude and latitude values as they would be huge otherwise. Are there some APIs or tools I can use for these conversions? And, if not, how else can I do it? It's also okay if the storage is done using floating point values or even just the fixed points would be fine as well. I just have to ensure that the rounding and its conversions go flawlessly. I've been using python, but I'm open to solutions in Java or alike also. Please help! TIA!
Nov 16, 2018 906 views

## 1 answer to this question.

So, if it's a double, it'd be easier translating it to 64-bits using long Double.doubleToLongBits(double value) and, converting it back using double Double.longBitsToDouble(long bits).

And, if its float values that you're converting to 32-bit values and then back again, you could use the functions, floatToIntBits(...) and intBitsToFloat(...).

But, for converting a double value to its 32 bits representation, you'd need a 2-step conversion, where you'd have to first convert it into a float value first:

```double val = ...;
int bits = Float.floatToIntBits( (float) val );```

And, with this, you convert it back:

```int bits = ...;
double val = (double) Float.intBitsToFloat(bits);```

Here's how the packing and unpacking directly to a buffer happens:

```ByteArrayOutputStream baos = new ByteArrayOutputStream(20);
DataOutputStream dos = new DataOutputStream( ... );
dos.writeFloat( altitude );
dos.writeFloat( max_temperature );
dos.writeFloat( longitude );
dos.writeFloat( latitude );
dos.writeFloat( average_acceleration);
byte[] data = baos.toByteArray();```

But, you'll have to pick only some selected values that you convert to 32 bits, and figure out the ones you can truncate to occupy lesser space as all of this still generates a 20 bytes buffer (5 x 32-bit floats) and not a 12 bytes buffer.

And, for unpacking, you should be using a DataInputStream and readFloat(), like this:

```ByteArrayInputStream bais = ...
DataInputStream dis = new DataInputStream(bais);
...etc...```
• 8,240 points

## Maximum number of BLE sensors that could be connected to a BLE gateway at a given instant?

The Bluetooth Classic has a limitation of ...READ MORE

## I want to perform Type Conversion of Date Type Value

Instead of returning return Long.toHexString(date.getTime()); Return following return Long.toHexString(date.getTime()/1000); As correctly ...READ MORE

+1 vote

## How to get percentage value of different gases using the MQ135 gas sensor

I don't think the MQ135 can do ...READ MORE

## Components of a BLE protocol

You have to search for the customized ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE