The sending C-side
发送C端
double tmp = htonl(anydouble);
send(socket, (char *) &tmp, sizeof(tmp), 0);
On the Java side, im reading the network data into a char[8]
在Java方面,我正在将网络数据读入char [8]
What is the proper way of performing the conversion back to double? Is it the way to go to simply send a string and parse it back to double?
执行转换的正确方法是什么?它是简单地发送一个字符串并将其解析为双倍的方法吗?
4 个解决方案
#1
1
I would advice to use Google Protocol Buffers: https://developers.google.com/protocol-buffers/docs/overview It has libs for C and Java and many other languages, like for example Python, which you may find useful later. It is efficient, robust, will take care of endianness, etc.
我建议使用Google Protocol Buffers:https://developers.google.com/protocol-buffers/docs/overview它包含用于C和Java以及许多其他语言的库,例如Python,您可能会在以后找到它们。它高效,强大,可以处理字节序等。
#2
3
This should work.
这应该工作。
char[] sentDouble = readBytes(); // get your bytes char[8]
String asString = new String(sentDouble); // make a string out of it
double myDouble = Double.parseDouble(asString); // parse it into a double
With a byte[] you can do
使用byte []可以做到
import java.nio.ByteBuffer;
public static double toDouble(byte[] bytes) {
return ByteBuffer.wrap(bytes).getDouble();
}
#3
1
You're reinterpreting a double as a long. This won't be portable. Here are some alternatives:
你正在重新解释一个双长。这不便携。以下是一些替代方案:
- do you really need to use floating point values? Can you represent your values as integers (e.g. milliseconds instead of seconds)?
- encode the double as a string (e.g. sprintf and Double.parseDouble)
- extract mantissa and exponent with frexp and send as integers, then convert back in Java with Double.longBitsToDouble
你真的需要使用浮点值吗?你能把你的值表示为整数(例如毫秒而不是秒)吗?
将double编码为字符串(例如sprintf和Double.parseDouble)
用frexp提取尾数和指数并作为整数发送,然后用Double.longBitsToDouble转换回Java
#4
0
There's no need for String
overhead. Assemble the bytes into a long
first, then use Double's static longBitsToDouble method.
不需要String开销。首先将字节组装成long,然后使用Double的静态longBitsToDouble方法。
// The bit pattern for the double "1.0", assuming most signficant bit first in array.
char[] charArray = new char[] {0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
long bytesAsLong = 0L;
for (int i = 0; i < 8; i++)
{
bytesAsLong |= ((long) charArray[i] << (56 - 8*i));
}
double value = Double.longBitsToDouble(bytesAsLong);
System.out.println(value); // prints "1.0"
#1
1
I would advice to use Google Protocol Buffers: https://developers.google.com/protocol-buffers/docs/overview It has libs for C and Java and many other languages, like for example Python, which you may find useful later. It is efficient, robust, will take care of endianness, etc.
我建议使用Google Protocol Buffers:https://developers.google.com/protocol-buffers/docs/overview它包含用于C和Java以及许多其他语言的库,例如Python,您可能会在以后找到它们。它高效,强大,可以处理字节序等。
#2
3
This should work.
这应该工作。
char[] sentDouble = readBytes(); // get your bytes char[8]
String asString = new String(sentDouble); // make a string out of it
double myDouble = Double.parseDouble(asString); // parse it into a double
With a byte[] you can do
使用byte []可以做到
import java.nio.ByteBuffer;
public static double toDouble(byte[] bytes) {
return ByteBuffer.wrap(bytes).getDouble();
}
#3
1
You're reinterpreting a double as a long. This won't be portable. Here are some alternatives:
你正在重新解释一个双长。这不便携。以下是一些替代方案:
- do you really need to use floating point values? Can you represent your values as integers (e.g. milliseconds instead of seconds)?
- encode the double as a string (e.g. sprintf and Double.parseDouble)
- extract mantissa and exponent with frexp and send as integers, then convert back in Java with Double.longBitsToDouble
你真的需要使用浮点值吗?你能把你的值表示为整数(例如毫秒而不是秒)吗?
将double编码为字符串(例如sprintf和Double.parseDouble)
用frexp提取尾数和指数并作为整数发送,然后用Double.longBitsToDouble转换回Java
#4
0
There's no need for String
overhead. Assemble the bytes into a long
first, then use Double's static longBitsToDouble method.
不需要String开销。首先将字节组装成long,然后使用Double的静态longBitsToDouble方法。
// The bit pattern for the double "1.0", assuming most signficant bit first in array.
char[] charArray = new char[] {0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
long bytesAsLong = 0L;
for (int i = 0; i < 8; i++)
{
bytesAsLong |= ((long) charArray[i] << (56 - 8*i));
}
double value = Double.longBitsToDouble(bytesAsLong);
System.out.println(value); // prints "1.0"