Reputation: 893
I have a PIC32 embedded microcontroller running the C32 compiler. I'm sending a UDP packet containing a 64-bit double value to a Java program. In my test, I'm trying to send the decimal value 40.5 to Java.
I would expect to receive: 0x4044400000000000
since System.out.println(Double.longBitsToDouble(0x4044400000000000L));
prints: 40.5
If I send the UDP packet without any conversions, I'm receiving: 0x00002242352D3330
Any idea how I can get this value into Java? I can't make heads or tails of the C32 representation of the double.
Upvotes: 1
Views: 446
Reputation: 989
The PIC32 is little endian. A double
is represented by 64 bits, and a long
is 32 bits. For 64 bits, you need a long long
.
htonl()
takes a long
, as noted above. You are truncating it with the cast, and after endianness conversion, you are getting the shown results.
I found this in a pastebin; it's macro that should work for converting your 64 bit long long
:
#define htonll(x) \
((((x) & 0xff00000000000000LL) >> 56) | \
(((x) & 0x00ff000000000000LL) >> 40) | \
(((x) & 0x0000ff0000000000LL) >> 24) | \
(((x) & 0x000000ff00000000LL) >> 8) | \
(((x) & 0x00000000ff000000LL) << 8) | \
(((x) & 0x0000000000ff0000LL) << 24) | \
(((x) & 0x000000000000ff00LL) << 40) | \
Upvotes: 2