Java: Bytes to floats / ints

前端 未结 5 1230
醉梦人生
醉梦人生 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

    Note: I recommend @Ophidian's ByteBuffer approach below, it's much cleaner than this. However this answer can be helpful in understanding the bit arithmetic going on.

    I don't know the endianness of your data. You basically need to get the bytes into an int type depending on the order of the bytes, e.g.:

    int asInt = (bytes[0] & 0xFF) 
                | ((bytes[1] & 0xFF) << 8) 
                | ((bytes[2] & 0xFF) << 16) 
                | ((bytes[3] & 0xFF) << 24);
    

    Then you can transform to a float using this:

    float asFloat = Float.intBitsToFloat(asInt);
    

    This is basically what DataInputStream does under the covers, but it assumes your bytes are in a certain order.

    Edit - On Bitwise OR

    The OP asked for clarification on what bitwise OR does in this case. While this is a larger topic that might be better researched independently, I'll give a quick brief. Or (|) is a bitwise operator whose result is the set of bits by individually or-ing each bit from the two operands.

    E.g. (in binary)

       10100000
    |  10001100
    -----------
       10101100
    

    When I suggest using it above, it involves shifting each byte into a unique position in the int. So if you had the bytes {0x01, 0x02, 0x03, 0x04}, which in binary is {00000001, 00000010, 00000011, 00000100}, you have this:

                                      0000 0001   (1)
                            0000 0010             (2 <<  8)
                  0000 0011                       (3 << 16)
      | 0000 0100                                 (4 << 24)
      --------------------------------------------------------
        0000 0100 0000 0011 0000 0010 0000 0001   (67 305 985)
    

    When you OR two numbers together and you know that no two corresponding bits are set in both (as is the case here), bitwise OR is the same as addition.

    See Also

    • Wikipedia: Bitwise OR
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-30 05:39

    Use a DataInputStream as follows:

        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
        float f = dis.readFloat();
    
        //or if it's an int:        
        int i = dis.readInt();
    
    0 讨论(0)
  • 2020-12-30 05:46

    In my opinion the ByteBuffer approach is better, you can specify where the data is wrapped (from which byte index, here 0) and also the endianness (here BIG_ENDIAN).

    try {
        float result = ByteBuffer.wrap(data, 0, 4).order(BIG_ENDIAN).getFloat();
    } catch (IndexOutOfBoundsException exception) {
        // TODO: handle exception
    }
    

    Functions similar to getFloat() exist for Int, Short, Long...

    0 讨论(0)
  • 2020-12-30 05:54

    You probably want to make use of java.nio.ByteBuffer. It has a lot of handy methods for pulling different types out of a byte array and should also handle most issues of endianness for you (including switching the byte order if necessary).

    byte[] data = new byte[36]; 
    //... populate byte array...
    
    ByteBuffer buffer = ByteBuffer.wrap(data);
    
    int first = buffer.getInt();
    float second = buffer.getFloat();
    

    It also has fancy features for converting your byte array to an int array (via an IntBuffer from the asIntBuffer() method) or float array (via a FloatBuffer from the asFloatBuffer() method) if you know that the input is really all of one type.

    0 讨论(0)
提交回复
热议问题