Java Float to Byte Array floatToBytes(float fnum, byte[] bytes, int startIndex)

Here you can find the source of floatToBytes(float fnum, byte[] bytes, int startIndex)

Description

translate float into bytes, stored in byte array starting from startIndex

License

Open Source License

Parameter

Parameter Description
num the float to be translated
bytes [] the byte array
startIndex starting to store in this index

Declaration

public static int floatToBytes(float fnum, byte[] bytes, int startIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from ww w.  ja  va 2s .  c o  m
     * translate float into bytes, stored in byte array starting from startIndex
     * 
     * @param num
     *            the float to be translated
     * @param bytes
     *            [] the byte array
     * @param startIndex
     *            starting to store in this index
     * @ret the index of the cell after storing the number.
     */
    public static int floatToBytes(float fnum, byte[] bytes, int startIndex) {
        return intToBytes(Float.floatToIntBits(fnum), bytes, startIndex);
    }

    /**
     * translate int into bytes, stored in byte array starting from startIndex
     * 
     * @param num
     *            the integer to be translated
     * @param bytes
     *            [] the byte array
     * @param startIndex
     *            starting to store in this index
     * @ret the index of the cell after storing the number.
     */
    public static int intToBytes(int num, byte[] bytes, int startIndex) {
        bytes[startIndex] = (byte) (num & 0xff);
        bytes[startIndex + 1] = (byte) ((num >> 8) & 0xff);
        bytes[startIndex + 2] = (byte) ((num >> 16) & 0xff);
        bytes[startIndex + 3] = (byte) ((num >> 24) & 0xff);
        return startIndex + 4;
    }
}

Related

  1. floatToByteArray(float source)
  2. floatToByteArray(float value)
  3. floatToByteArrayBE(float data)
  4. floatToBytes(final float f)
  5. floatToBytes(float f)
  6. floatToBytes(float num, byte[] arr, int pos)
  7. floatToBytes(float theFloat)
  8. FloatToBytesLE(final float value)
  9. floatToBytesLE(float f, byte[] bytes, int off)