I am trying to convert a float
into a primitive byte[]
and vice versa:
public byte[] floatToByteArray(final float value)
{
return new byte[]
{
(byte) (value >> 56),
(byte) (value >> 48),
(byte) (value >> 40),
(byte) (value >> 32),
(byte) (value >> 24),
(byte) (value >> 16),
(byte) (value >> 8),
(byte) (value)
};
}
Oddly enough, when I try to shift the newly allocated byte[]
back into a float
, the result is nothing but rubbish.
However, it would appear as if the same algorithm works just fine when I use the primitive long
data type as argument instead.
public byte[] longToByteArray(final long value)
{
return new byte[]
{
(byte) (value >> 56),
(byte) (value >> 48),
(byte) (value >> 40),
(byte) (value >> 32),
(byte) (value >> 24),
(byte) (value >> 16),
(byte) (value >> 8),
(byte) (value)
};
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…