 <p>Use <code>java.nio.ByteBuffer</code></p> <p>Something like:</p> <pre><code>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(); } </code></pre>