1
int port = 18000;
DatagramSocket serverSocket = new DatagramSocket(port);
byte[] receiveData = new byte[8];

System.out.println("Server Listing on Port: "+port);
String x;

while (true){
  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  serverSocket.receive(receivePacket);

?? 双 x = 新的 Doulbe.parseBytes(recievedPacket)

4

4 回答 4

2

你可以使用ByteBuffer

Double x = ByteBuffer.wrap(receiveData).getDouble();
于 2012-12-28T14:38:18.493 回答
1

利用java.nio.ByteBuffer

就像是:

private static ByteBuffer buffer = ByteBuffer.allocate(8);    

public static byte[] encodeDouble(double x) {
    buffer.clear();
    buffer.putDouble(0, x);
    return buffer.array();
}

public static double decodeDouble(byte[] bytes) {
    buffer.clear();
    buffer.put(bytes);
    buffer.flip(); 
    return buffer.getDouble();
}    
于 2012-12-28T14:40:16.140 回答
1
ByteBuffer.wrap(receiveData.getData()).getDouble();
于 2012-12-28T14:40:18.360 回答
0
java.io.DataInputStream.readDouble()
于 2012-12-28T15:04:04.520 回答