Transform De-transforming 32-bit representation of a double value

0 votes
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 in IoT (Internet of Things) by Bharani
• 4,660 points
1,188 views

1 answer to this question.

0 votes

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);
altitude = dis.readFloat();
max_temperature = dis.readFloat();
...etc...
answered Nov 16, 2018 by DataKing99
• 8,250 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer
0 votes
1 answer

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

answered Aug 4, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
783 views
+1 vote
1 answer
0 votes
1 answer

Components of a BLE protocol

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

answered Sep 27, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,222 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

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

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,443 views
0 votes
1 answer
0 votes
1 answer

RPMs for IoT Agents of Backend Device Management GE in FIWARE IoT ecosystem

The RPMs for IDAS component are availaible. ...READ MORE

answered Jul 30, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
824 views
0 votes
1 answer

Running a childProcess as shell script with node.js server

Here's what I think, you could pass ...READ MORE

answered Aug 14, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
764 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP