Here you can find the source of toByteArr(final float value, final boolean lsbIsFirst)
float
to byte[4]
.
Parameter | Description |
---|---|
value | The <code>float</code> value that must be converted |
lsbIsFirst | If the low Byte at first Postion at <code>byte[]</code> then select <code>true</code> or if its at last postion in the <code>byte[]</code> then select <code>false</code> |
byte[4]
public static byte[] toByteArr(final float value, final boolean lsbIsFirst)
//package com.java2s; /**/*from w w w .j a va2 s .c o m*/ * jModuleConnect is an framework for communication and file management on modem * modules. * * This project was inspired by the project TC65SH * by Christoph Vilsmeier: <http://www.vilsmeier-consulting.de/tc65sh.html> * * Copyright (C) 2015 sitec systems GmbH <http://www.sitec-systems.de> * * This file is part of jModuleConnect. * * jModuleConnect is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your option) * any later version. * * jModuleConnect is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with jModuleConnect. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Converts a <code>float</code> to <code>byte[4]</code>. * @param value The <code>float</code> value that must be converted * @param lsbIsFirst If the low Byte at first Postion at <code>byte[]</code> * then select <code>true</code> or if its at last postion in the * <code>byte[]</code> then select <code>false</code> * @return The converted <code>byte[4]</code> * @since 1.0 */ public static byte[] toByteArr(final float value, final boolean lsbIsFirst) { return toByteArr(Float.floatToIntBits(value), false); } /** * Converts a <code>int</code> value to a <code>byte[4]</code>. * @param value The <code>int</code> value that must be converted * @param lsbIsFirst lsbIsFirst If the low Byte at first postion at * <code>byte[]</code> then select <code>true</code> or if its at last * Postion in the <code>byte[]</code> then select <code>false</code> * @return The converted <code>byte[4]</code> * @since 1.0 */ public static byte[] toByteArr(final int value, final boolean lsbIsFirst) { byte[] result = new byte[4]; putToByteArr(result, value, 0, lsbIsFirst); return result; } /** * Converts a <code>long</code> value to a <code>byte[8]</code>. * @param value The <code>int</code> value that must be converted * @param lsbIsFirst lsbIsFirst If the low Byte at first postion at * <code>byte[]</code> then select <code>true</code> or if its at last * postion in the <code>byte[]</code> then select <code>false</code> * @return The converted <code>byte[8]</code> * @since 1.0 */ public static byte[] toByteArr(final long value, final boolean lsbIsFirst) { byte[] result = new byte[8]; putToByteArr(result, value, 0, lsbIsFirst); return result; } /** * Converts a <code>short</code> Value to a <code>byte[2]</code>. * @param value The <code>short</code> value that must be converted * @param lsbIsFirst lsbIsFirst If the low Byte at first postion at * <code>byte[]</code> then select <code>true</code> or if its at last * postion in the <code>byte[]</code> then select <code>false</code> * @return The converted <code>byte[2]</code> * @since 1.0 */ public static byte[] toByteArr(final short value, final boolean lsbIsFirst) { byte[] result = new byte[2]; putToByteArr(result, value, 0, lsbIsFirst); return result; } /** * Converts and puts a <code>float</code> to the input <code>byte[]</code>. * @param inArr The <code>byte[]</code> in that the method add the converted * value * @param value The <code>float</code> value that must be converted * @param offset The offset after that the converted value will at in the * input array * @param lsbIsFirst If the low Byte at first Postion at <code>byte[]</code> * then select <code>true</code> or if its at last postion in the * <code>byte[]</code> then select <code>false</code> * @since 1.0 */ public static void putToByteArr(final byte[] inArr, final float value, final int offset, final boolean lsbIsFirst) { putToByteArr(inArr, Float.floatToIntBits(value), offset, lsbIsFirst); } /** * Converts and puts a <code>int</code> to the input <code>byte[]</code>. * @param inArr The <code>byte[]</code> in that the method add the converted * value * @param value The <code>int</code> value that must be converted * @param offset The offset after that the converted value will at in the * input array * @param lsbIsFirst If the low Byte at first Postion at <code>byte[]</code> * then select <code>true</code> or if its at last postion in the * <code>byte[]</code> then select <code>false</code> * @since 1.0 */ public static void putToByteArr(final byte[] inArr, final int value, final int offset, final boolean lsbIsFirst) { nullCheck(inArr, "inArr"); minLengthCheck(inArr, (byte) 4); if (lsbIsFirst) { inArr[offset] = (byte) value; inArr[offset + 1] = (byte) (value >>> 8); inArr[offset + 2] = (byte) (value >>> 16); inArr[offset + 3] = (byte) (value >>> 24); } else { inArr[offset] = (byte) (value >>> 24); inArr[offset + 1] = (byte) (value >>> 16); inArr[offset + 2] = (byte) (value >>> 8); inArr[offset + 3] = (byte) value; } } /** * Converts and puts a <code>long</code> to the input <code>byte[]</code>. * @param inArr The <code>byte[]</code> in that the method add the converted * value * @param value The <code>long</code> value that must be converted * @param offset The offset after that the converted value will at in the * input array * @param lsbIsFirst If the low Byte at first Postion at <code>byte[]</code> * then select <code>true</code> or if its at last postion in the * <code>byte[]</code> then select <code>false</code> * @since 1.0 */ public static void putToByteArr(final byte[] inArr, final long value, final int offset, final boolean lsbIsFirst) { nullCheck(inArr, "inArr"); minLengthCheck(inArr, (byte) 8); if (lsbIsFirst) { inArr[offset] = (byte) value; inArr[offset + 1] = (byte) (value >>> 8); inArr[offset + 2] = (byte) (value >>> 16); inArr[offset + 3] = (byte) (value >>> 24); inArr[offset + 4] = (byte) (value >>> 32); inArr[offset + 5] = (byte) (value >>> 40); inArr[offset + 6] = (byte) (value >>> 48); inArr[offset + 7] = (byte) (value >>> 56); } else { inArr[offset] = (byte) (value >>> 56); inArr[offset + 1] = (byte) (value >>> 48); inArr[offset + 2] = (byte) (value >>> 40); inArr[offset + 3] = (byte) (value >>> 32); inArr[offset + 4] = (byte) (value >>> 24); inArr[offset + 5] = (byte) (value >>> 16); inArr[offset + 6] = (byte) (value >>> 8); inArr[offset + 7] = (byte) value; } } /** * Converts and puts a <code>short</code> to the input <code>byte[]</code>. * @param inArr The <code>short</code> in that the method add the converted * value * @param value The <code>float</code> value that must be converted * @param offset The offset after that the converted value will at in the * input array * @param lsbIsFirst If the low Byte at first Postion at <code>byte[]</code> * then select <code>true</code> or if its at last postion in the * <code>byte[]</code> then select <code>false</code> * @since 1.0 */ public static void putToByteArr(final byte[] inArr, final short value, final int offset, final boolean lsbIsFirst) { nullCheck(inArr, "inArr"); minLengthCheck(inArr, (byte) 2); if (lsbIsFirst) { inArr[offset] = (byte) value; inArr[offset + 1] = (byte) (value >>> 8); } else { inArr[offset] = (byte) (value >>> 8); inArr[offset + 1] = (byte) value; } } private static void nullCheck(final byte[] buffer, final String parameter) { if (buffer == null) { throw new NullPointerException("Parameter " + parameter + " cant be null"); } } private static void minLengthCheck(final byte[] buffer, final byte length) { if (buffer.length < length) { throw new IllegalArgumentException("Input array length must be min " + length + " bytes"); } } }