Here you can find the source of byteArrayToFloat(byte[] bytes, int size, int fpBits)
Parameter | Description |
---|---|
bytes | An array of bytes no smaller than the size to be converted |
size | Number of bytes to convert to the float. May not exceed 8. |
fpBits | Number of bits representing the decimal |
public static float byteArrayToFloat(byte[] bytes, int size, int fpBits)
//package com.java2s; /**//from w w w.j av a2 s.c o m * Oshi (https://github.com/dblock/oshi) * * Copyright (c) 2010 - 2016 The Oshi Project Team * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Maintainers: * dblock[at]dblock[dot]org * widdis[at]gmail[dot]com * enrico.bianchi[at]gmail[dot]com * * Contributors: * https://github.com/dblock/oshi/graphs/contributors */ public class Main { /** * Convert a byte array to its floating point representation. * * @param bytes * An array of bytes no smaller than the size to be converted * @param size * Number of bytes to convert to the float. May not exceed 8. * @param fpBits * Number of bits representing the decimal * @return A float; the integer portion representing the byte array as an * integer shifted by the bits specified in fpBits; with the * remaining bits used as a decimal */ public static float byteArrayToFloat(byte[] bytes, int size, int fpBits) { return byteArrayToLong(bytes, size) / (float) (1 << fpBits); } /** * Convert a byte array to its integer representation. * * @param bytes * An array of bytes no smaller than the size to be converted * @param size * Number of bytes to convert to the long. May not exceed 8. * @return An integer representing the byte array as a 64-bit number */ public static long byteArrayToLong(byte[] bytes, int size) { if (size > 8) { throw new IllegalArgumentException("Can't convert more than 8 bytes."); } if (size > bytes.length) { throw new IllegalArgumentException("Size can't be larger than array length."); } long total = 0L; for (int i = 0; i < size; i++) { total = total << 8 | bytes[i] & 0xff; } return total; } }