Android Byte Array to Float Convert getFloat(byte[] buf, boolean bigEndian)

Here you can find the source of getFloat(byte[] buf, boolean bigEndian)

Description

Convert byte sequence into float short from first 4 bytes

Parameter

Parameter Description
buf the source byte array
bigEndian true if big endian, false if little endian

Return

short the java float

Declaration

public static final float getFloat(byte[] buf, boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from ww w. j a v  a  2s  .com*/
     * Convert byte sequence into float short from first 4 bytes
     *
     * @param buf the source byte array
     * @param bigEndian true if big endian, false if little endian
     * @return short the java float
     */
    public static final float getFloat(byte[] buf, boolean bigEndian) {
        return Float.intBitsToFloat(getInt(buf, bigEndian));
    }

    /**
     * Convert byte sequence into java short from first 4 bytes
     *
     * @param buf the source byte array
     * @param bigEndian true if big endian, false if little endian
     * @return short the java short
     */
    public static final int getInt(byte[] buf, boolean bigEndian) {
        return getInt(buf, 0, bigEndian);
    }

    /**
     * Convert byte sequence into java int.
     *
     * @param buf the source byte array
     * @param pos the position of array to convert from
     * @param bigEndian true if big endian, false if little endian
     * @return short the java int
     */
    public static final int getInt(byte[] buf, int pos, boolean bigEndian) {
        if (bigEndian) {
            return ((buf[pos] & 0xff) << 24)
                    | ((buf[pos + 1] & 0xff) << 16)
                    | ((buf[pos + 2] & 0xff) << 8) | (buf[pos + 3] & 0xff);
        } else {
            return ((buf[pos + 3] & 0xff) << 24)
                    | ((buf[pos + 2] & 0xff) << 16)
                    | ((buf[pos + 1] & 0xff) << 8) | (buf[pos] & 0xff);
        }
    }
}

Related

  1. bytesLE2floats(byte[] b)
  2. bytesLE2floats2doubles(byte[] b)
  3. bytesToFloat(byte[] bytes)
  4. bytesToFloat(byte[] bytes, int start)
  5. getFloat(byte[] b, int index)
  6. getFloat(byte[] bytes)
  7. getFloat(int offset, byte[] fromBytes)
  8. getFloats(float[] toFloats, byte[] fromBytes)
  9. getFloats(float[] toFloats, int offset, byte[] fromBytes)