Java ByteBuffer to Byte Array getBytes(ByteBuffer floatCalculator, float number)

Here you can find the source of getBytes(ByteBuffer floatCalculator, float number)

Description

Calculates the 4 sequential bytes given the corresponding float value

License

LGPL

Parameter

Parameter Description
floatCalculator Buffer to do the conversion
number The float value

Return

The corresponding sequence of 4 bytes

Declaration

public static byte[] getBytes(ByteBuffer floatCalculator, float number) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.nio.ByteBuffer;

public class Main {
    /**/* w  w w.j  a va 2  s  .  co m*/
     * Calculates the 4 sequential bytes given the corresponding float value
     * @param floatCalculator Buffer to do the conversion
     * @param number The float value
     * @return The corresponding sequence of 4 bytes
     */
    public static byte[] getBytes(ByteBuffer floatCalculator, float number) {
        byte[] bytes = new byte[4];
        floatCalculator.clear();
        floatCalculator.putFloat(number);
        floatCalculator.position(0);
        floatCalculator.get(bytes);
        return bytes;
    }

    /**
     * Calculates the 4 sequential bytes given the corresponding float value
     * @param longCalculator Buffer to do the conversion
     * @param number The float value
     * @return The corresponding sequence of 4 bytes
     */
    public static byte[] getBytes(ByteBuffer longCalculator, long number) {
        byte[] bytes = new byte[8];
        longCalculator.clear();
        longCalculator.putLong(number);
        longCalculator.position(0);
        longCalculator.get(bytes);
        return bytes;
    }

    /**
     * Calculates the 4 sequential bytes given the corresponding integer value.
     * @param intCalculator Buffer to do the conversion
     * @param number The integer value
     * @return The corresponding sequence of 4 bytes
     */
    public static byte[] getBytes(ByteBuffer intCalculator, int number) {
        byte[] bytes = new byte[4];
        intCalculator.clear();
        intCalculator.putInt(number);
        intCalculator.position(0);
        intCalculator.get(bytes);
        return bytes;
    }
}

Related

  1. getBytes(ByteBuffer buf, byte[] arr)
  2. getBytes(ByteBuffer buf, int length)
  3. getBytes(ByteBuffer buffer, int len)
  4. getBytes(ByteBuffer byteBuffer, int length)
  5. getBytes(ByteBuffer bytes)
  6. getBytes(ByteBuffer in)
  7. getBytes(MappedByteBuffer buffer, int pos, byte[] target)
  8. getBytesAtOffset(ByteBuffer buffer, int offset, int length)
  9. getBytesFromBuffer(ByteBuffer buffer, int len)