Java ByteBuffer Read readHealthFloat16(ByteBuffer data)

Here you can find the source of readHealthFloat16(ByteBuffer data)

Description

Reads a decimal value as a IEEE-11073 16-bits float

License

Mozilla Public License

Parameter

Parameter Description
data byte buffer to read the 2 bytes from

Return

java float value decoded from the given bytes

Declaration

public static float readHealthFloat16(ByteBuffer data) 

Method Source Code

//package com.java2s;
/***************************** BEGIN LICENSE BLOCK ***************************
    /* w w  w .  j  a  v a 2s. c  om*/
The contents of this file are subject to the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
    
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
     
Copyright (C) 2012-2016 Sensia Software LLC. All Rights Reserved.
     
******************************* END LICENSE BLOCK ***************************/

import java.nio.ByteBuffer;

public class Main {
    /**
     * Reads a decimal value as a IEEE-11073 16-bits float
     * @param data byte buffer to read the 2 bytes from
     * @return java float value decoded from the given bytes
     */
    public static float readHealthFloat16(ByteBuffer data) {
        byte b0 = data.get();
        byte b1 = data.get();
        int mantissa = unsignedToSigned((b0 & 0xFF) + ((b1 & 0x0F) << 8), 12);
        int exponent = unsignedToSigned((b1 & 0xFF) >> 4, 4);
        return (float) (mantissa * Math.pow(10, exponent));
    }

    private static int unsignedToSigned(int unsigned, int size) {
        if ((unsigned & (1 << size - 1)) != 0)
            unsigned = -1 * ((1 << size - 1) - (unsigned & ((1 << size - 1) - 1)));
        return unsigned;
    }
}

Related

  1. readFixedLengthString(ByteBuffer byteBuffer, int size)
  2. readFixedPoint88(ByteBuffer bb)
  3. readFourByteInt(ByteBuffer buffer, int start)
  4. readFrom(File file, ByteBuffer buffer)
  5. readFully(final FileChannel src, final ByteBuffer dst, final long position)
  6. readHexString(ByteBuffer buffer, int nrBytes)
  7. readIso639(ByteBuffer bb)
  8. readKatakana(ByteBuffer b, char s[], int off, int len)
  9. readLen(ByteBuffer dup, int nls)