Java: Bytes to floats / ints

前端 未结 5 1254
醉梦人生
醉梦人生 2020-12-30 05:21

I have a byte array sent via UDP from x-plane. The bytes (4) are all floats or integers… I tried to cast them to floats but no luck so far…

Example array: byte d

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 05:29

    You cannot just cast them into a float/int. You have to convert the bytes into an int or float.

    Here is one simple way to do it:

    byte [] data = new byte[] {1,2,3,4};
    ByteBuffer b = ByteBuffer.wrap(data);
    System.out.println(b.getInt());
    System.out.println(b.getFloat());
    

    There is a reasonable discussion here:

    http://www.velocityreviews.com/forums/t129791-convert-a-byte-array-to-a-float.html

提交回复
热议问题