Convert float[] to byte[] to float[] again

前端 未结 8 1645
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 15:17

So what I\'m trying to do here is get a float[], convert it to byte[], send it through the network as a datagram packet and then convert it back to

8条回答
  •  無奈伤痛
    2020-12-31 15:57

    ByteBuffer document: https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html

    final static int nbBytesInFloat = Float.SIZE / Byte.SIZE;
    
    public static byte[] toByteArray(float[] floatArray){
        byte[] result = new byte[floatArray.length * nbBytesInFloat];
        ByteBuffer wrappedBytes = ByteBuffer.wrap(result);
        for(int i=0;i

提交回复
热议问题