0

I am transmitting a double value from a C#-mqtt client to a Java-mqtt client. Mqtt requires its payload to be a byte[] so I in c# I am doing the following:

byte[] vals = BitConverter.GetBytes(sub.value); // c#-sender

and transmitting this over mqtt to a java client, which in turn

double result = ByteBuffer.wrap(vals).getDouble(); // java-receiver

However, while the original double-value is in the range of ~1 to 10, the resulting java value is in the range of 10^-311 to 10^-312.

I am not very familiar with c# at this point and can't find the problem.

Is it an offset problem? LE/BE ? I am pretty much stuck and would love if you could give me a hint.

Aeefire
  • 888
  • 10
  • 25
  • Don't know about C#, but you can use [`Double.doubleToLongBits`](https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#doubleToLongBits(double)) and suitable printing to visualize the bits of the `double`; might help you to pin down whether endianness is the issue. – Andy Turner Nov 18 '16 at 09:06
  • Java ByteBuffer also has the `order(ByteOrder or)` method which can be used to flip the order, it should be a one line insert to test if it's a ordering problem – hardillb Nov 18 '16 at 09:15
  • hardillb hah! thanks for the tip. .order(ByteOrder.LITTLE_ENDIAN) did the trick! Can you add an answer? – Aeefire Nov 18 '16 at 09:22
  • @Aeefire answer added – hardillb Nov 18 '16 at 12:25

1 Answers1

0

As mentioned in the comments, try flipping the byte order on the ByteBuffer using the ByteBuffer.order(ByteOrder) method

hardillb
  • 54,545
  • 11
  • 67
  • 105