Java ByteOrder toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)

Here you can find the source of toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)

Description

to Float Byte Array

License

Apache License

Declaration

public static final int toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)
            throws NumberFormatException 

Method Source Code

//package com.java2s;
/*/*from w  w w.j  a  v a2 s.co m*/
 * Copyright 2013 Lyor Goldstein
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.ByteOrder;

public class Main {
    public static final int toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)
            throws NumberFormatException {
        return toInt32ByteArray(Float.floatToIntBits(val), outOrder, buf, off);
    }

    public static final int toFloatByteArray(float val, ByteOrder outOrder, byte[] buf)
            throws NumberFormatException {
        return toFloatByteArray(val, outOrder, buf, 0);
    }

    public static final byte[] toFloatByteArray(float val, ByteOrder outOrder) throws NumberFormatException {
        return toInt32ByteArray(Float.floatToIntBits(val), outOrder);
    }

    public static final int toInt32ByteArray(final int val, final ByteOrder outOrder, final byte[] buf,
            final int off) throws NumberFormatException {
        if (null == outOrder)
            throw new NumberFormatException("toInt32ByteArray(" + val + ") no order specified");

        final boolean isBigEndian = ByteOrder.BIG_ENDIAN.equals(outOrder);
        buf[off] = (byte) ((isBigEndian ? (val >> 24) : val) & 0x00FF);
        buf[off + 1] = (byte) ((isBigEndian ? (val >> 16) : (val >> 8)) & 0x00FF);
        buf[off + 2] = (byte) ((isBigEndian ? (val >> 8) : (val >> 16)) & 0x00FF);
        buf[off + 3] = (byte) ((isBigEndian ? val : (val >> 24)) & 0x00FF);

        return 4;
    }

    public static final int toInt32ByteArray(final int val, final ByteOrder outOrder, final byte[] buf)
            throws NumberFormatException {
        return toInt32ByteArray(val, outOrder, buf, 0);
    }

    public static final byte[] toInt32ByteArray(final int val, final ByteOrder outOrder)
            throws NumberFormatException {
        final byte[] data = new byte[4];
        final int eLen = toInt32ByteArray(val, outOrder, data);
        if (eLen != data.length)
            throw new NumberFormatException("toInt32ByteArray(" + val + ")[" + outOrder
                    + "] unexpected used length: expected=" + data.length + "/got=" + eLen);
        return data;
    }
}

Related

  1. opposite(ByteOrder order)
  2. reduceToSmallestByteArray(byte[] originalBytes, ByteOrder byteOrder, boolean isSigned)
  3. setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder)
  4. toBytes(int value, ByteOrder order)
  5. toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)
  6. toInt(byte[] bytes, ByteOrder order)
  7. toInt16ByteArray(final int val, final ByteOrder outOrder, final byte[] buf, final int off)
  8. toInt64ByteArray(final long val, final ByteOrder outOrder, final byte[] buf)