With reference to (Double to byte[]) It contains both the question and the solution I require.
private byte[] convert(Double d) {
byte[] output = new byte[8];
Long lng = Double.doubleToLongBits(d);
for (int i = 0; i < 8; i++) {
output[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
}
return output;
}
But I don't know how/why it works, I understand the loop is assigning each of the 8 bytes that make up a Double to the array. but I don't understand how the byte is being constructed. Could someone please explain to me the following expression?
((lng >> ((7 - i) * 8)) & 0xff);
and why is Double.doubleToLongBits(d); getting involved?