java float数组转byte数组,byte数组转float
首先是float数组转byte数组
public static byte[] FloatArrayToByteArray(float[] data)
{
byte[] Resutl = {};
for (int i = 0; i < data.length; i++)
{
byte[] intToBytes2 = intToBytes2(Float.floatToIntBits(data[i]));
byte[] temp = new byte[4];
temp[0] = intToBytes2[3];
temp[1] = intToBytes2[2];
temp[2] = intToBytes2[1];
temp[3] = intToBytes2[0];
Resutl = concat(Resutl,temp);
}
return Resutl;
}
拼接byte数组
public static byte[] concat(byte[] a, byte[] b) {
byte[] c= new byte[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
将byte数组转成float数组
public static float[] ByteArrayToFloatArray(byte[] data)
{
float[] result = new float[data.length / 4];
int temp = 0;
for (int i = 0; i < data.length; i += 4)
{
temp = temp | (data[i] & 0xff) << 0;
temp = temp | (data[i+1] & 0xff) << 8;
temp = temp | (data[i+2] & 0xff) << 16;
temp = temp | (data[i+3] & 0xff) << 24;
result[i / 4] = Float.intBitsToFloat(temp);
}
return result;
}